Intro of Java CHAPTER 1 PDF
Intro of Java CHAPTER 1 PDF
INTRODUCTION TO JAVA
Java is an object-oriented programming language with its runtime environment. It is a
combination of features of C and C++ with some essential additional concepts. Java is well suited
for both standalone and web application development and is designed to provide solutions to
most of the problems faced by users of the internet era.
❖ What is java?
• Java is an object-oriented programming language developed by Sun Microsystems, and it
was released in 1991.
• James Gosling initially developed Java in Sun Microsystems (which was later merged
with Oracle Corporation).
• Java is a set of features of C and C++. It has obtained its format from C, and OOP features from
C++.
• Java programs are platform independent which means they can be run on any operating
system with any processor as long as the Java interpreter is available on that system.
• Java code that runs on one platform does not need to be recompiled to run on another
platform; it's called write once, run anywhere(WORA).
• Java Virtual Machine (JVM) executes Java code, but it has been written in platform-specific
languages such as C/C++/ASM, etc. JVM is not written in Java and hence cannot be platform
independent, and Java interpreter is a part of JVM.
❖ Features of JAVA:
Simple
Java is very easy to learn and its syntax is simple, clean and easy to understand. According to
Sun, Java language is a simple programming language because:
o Java syntax is based on C++ (so easier for programmers to learn it after C++).
o Java has removed many confusing and rarely-used features e.g. explicit pointers, operator
overloading etc.
o There is no need to remove unreferenced objects because there is Automatic Garbage
Collection in java.
Object-oriented
Java is object-oriented programming language. Everything in Java is an object. Object-oriented
means we organize our software as a combination of different types of objects that incorporates
both data and behaviour.
Object-oriented programming (OOPs) is a methodology that simplifies software development
and maintenance by providing some rules.
Basic concepts of OOPs are:
1. Object
2. Class
3. Inheritance
4. Polymorphism
5. Abstraction
6. Encapsulation
Platform Independent
Java is platform independent because it is different from other languages like C, C++ etc. which
are compiled into platform specific machines while Java is a write once, run anywhere language.
A platform is the hardware or software environment in which a program runs.
There are two types of platforms software-based and hardware-based. Java provides software-
based platform.
The Java platform differs from most other platforms in the sense that it is a software-based
platform that runs on the top of other hardware-based platforms. It has two components:
1. Runtime Environment
2. API(Application Programming Interface)
Java code can be run on multiple platforms e.g. Windows, Linux, Sun Solaris, Mac/OS etc. Java
code is compiled by the compiler and converted into bytecode. This bytecode is a platform-
independent code because it can be run on multiple platforms i.e. Write Once and Run Anywhere
(WORA).
Secured
Java is best known for its security. With Java, we can develop virus-free systems. Java is secured
because:
o No explicit pointer
o Java Programs run inside virtual machine sandbox
o Classloader: Classloader in Java is a part of the Java Runtime Environment(JRE) which is
used to dynamically load Java classes into the Java Virtual Machine. It adds security by
separating the package for the classes of the local file system from those that are imported
from network sources.
o Bytecode Verifier: It checks the code fragments for illegal code that can violate access right
to objects.
o Security Manager: It determines what resources a class can access such as reading and
writing to the local disk.
These security are provided by java language. Some security can also be provided by application
developer through SSL, JAAS, Cryptography etc.
Robust
Robust simply means strong. Java is robust because:
o It uses strong memory management.
o There are lack of pointers that avoids security problems.
o There is automatic garbage collection in java which runs on the Java Virtual Machine to get
rid of objects which are not being used by a Java application anymore.
o There is exception handling and type checking mechanism in java. All these points makes
java robust.
Architecture-neutral
Java is architecture neutral because there is no implementation dependent features e.g. size of
primitive types is fixed.
In C programming, int data type occupies 2 bytes of memory for 32-bit architecture and 4 bytes
of memory for 64-bit architecture. But in java, it occupies 4 bytes of memory for both 32 and 64
bit architectures.
Portable
Java is portable because it facilitates you to carry the java byte code to any platform. It doesn't
require any type of implementation.
High-performance
Java is faster than other traditional interpreted programming languages because Java bytecode
is "close" to native code. It is still a little bit slower than a compiled language (e.g. C++). Java is
an interpreted language that is why it is slower than compiled languages e.g. C, C++ etc.
Distributed
Java is distributed because it facilitates users to create distributed applications in java. RMI and
EJB are used for creating distributed applications. This feature of Java makes us able to access
files by calling the methods from any machine on the internet.
Multi-threaded
A thread is like a separate program, executing concurrently. We can write Java programs that
deal with many tasks at once by defining multiple threads. The main advantage of multi-
threading is that it doesn't occupy memory for each thread. It shares a common memory area.
Threads are important for multi-media, Web applications etc.
Dynamic
Java is a dynamic language. It supports dynamic loading of classes. It means classes are loaded
on demand. It also supports functions from its native languages i.e. C and C++.
Java supports dynamic compilation and automatic memory management (garbage collection).
Text
Editor
Java source Javado HTML
code files
Javac
Java Jdb
Java
progra
m
output
❖ Difference between Java and C++
• Java does not include the C unique statement keywords goto, sizeof, and typedef.
• Java does not contain the data types struct, union and enum.
• Java does not support an explicit pointer type.
• Java does not have a preprocessor and therefore we cannot use # define, #include, and # ifdef
statements.
• Java does not support operator overloading.
• Java does not have template classes as in C+ + .
• Java does not support multiple inheritance of classes. This is accomplished using a new
feature called "interface".
• Java does not support global variables. Every variable and method is declared within a class
and forms part of that class.
• Java does not use pointers.
• Java has replaced the destructor function with a finalize( ) function.
Application Programming Interface
The Java Standard Library (or API) includes hundreds of classes and methods grouped into
several functional packages. Most commonly used packages are
Language Support Package: A collection of classes and methods required for implementing
basic feature of Java.
Utilities Package: A collection of classes to provide utility functions such as Date and Time.
Input/output Package: A collection of classes required for input and output manipulation.
Networking Package: A collection of classes for communicating with other computers via
Internet.
AWT Package: The abstract window toolkit package contains classes that implements platform
independent graphical user interface.
Applet Package: This includes a set of classes that allows us to create Java applets.
❖ Data Types
Data types specify the different sizes and values that can be stored in the variable. There are two
types of data types in Java:
1. Primitive data types: The primitive data types include Integer, Character, Boolean, and
Floating Point.
2. Non-primitive data types: The non-primitive data types include Classes, Interfaces, and
Arrays.
Primitive Data Types
In Java language, primitive data types are the building blocks of data manipulation. These are the
most basic data types available in Java language.
There are 8 types of primitive data types:
o boolean data type
o byte data type
o char data type
o short data type
o int data type
o long data type
o float data type
o double data type
Data Type Default Value Default size
boolean false 1 bit
char '\u0000' 2 byte
byte 0 1 byte
short 0 2 byte
int 0 4 byte
long 0L 8 byte
float 0.0f 4 byte
double 0.0d 8 byte
❖ Variables
A variable is a container which holds the value while the java program is executed. A variable is
assigned with a datatype.
Variable is a name of memory location. There are three types of variables in java: local, instance
and static.
There are two types of data types in java: primitive and non-primitive.
Variable
Variable is name of reserved area allocated in memory. In other words, it is a name of memory
location. It is a combination of "vary + able" that means its value can be changed.
int data=50;//Here data is variable
❖ Operators
Operator in java is a symbol that is used to perform operations. For example: +, -, *, / etc.
There are many types of operators in java which are given below:
• Increment / Decrement operators
• Arithmetic Operator,
• Relational Operator,
• Bitwise Operator,
• Logical Operator,
• Ternary Operator and
• Assignment Operator.
Operator
Category Precedence
Type
Increment/ postfix expr++ expr--
Decrement
prefix ++expr --expr
Arithmetic multiplicative */%
additive +-
Relational comparison < > <= >= instanceof
equality == !=
Bitwise bitwise AND &
bitwise exclusive OR ^
bitwise inclusive OR |
Logical logical AND &&
logical OR ||
Ternary ternary ?:
Assignment assignment = += -= *= /= %= &= ^= |= <<= >>= >>>=
❖ Comments
The java comments are statements that are not executed by the compiler and interpreter. The
comments can be used to provide information or explanation about the variable, method, class
or any statement. It can also be used to hide program code for specific time.
Types of Comments
There are 2 types of comments in java.
1. Single Line Comment
2. Multi Line Comment
import java.util.Scanner;
Enter an integer: 23
You entered 23
Here, input object of Scanner class is created. Then, the nextInt() method of the Scanner class is
used to get integer input from the user.
To get long, float, double and String input from the user, you can
use nextLong(), nextFloat(), nextDouble() and next() methods respectively.
Example 6: Get float, double and String Input
import java.util.Scanner;
class Input
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
❖ Array
Normally, array is a collection of similar type of elements that have contiguous memory location.
Java array is an object the contains elements of similar data type. It is a data structure where
we store similar elements. We can store only fixed set of elements in a java array.
Array in java is index based; first element of the array is stored at 0 index.
//printing 2D array
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
}
}
❖ String:
Strings are sequences of characters, such as "Hello". Java does not have a built-in string type.
Instead, the standard Java library contains a predefined class called, String. Each quoted string
is an instance of the String class:
String e = “” ; // an empty string
String greeting=”Hello”;
Methods Of String class:
java.lang.String
1) char charAt(int index)
returns the character at the specified location,
2) int compareTo(String other)
returns a negative value if the string comes before other in dictionary order, a positive value if
the string comes after other in dictionary order, or 0 if the strings are equal.
3) boolean equals(String other)
returns true if the string is equals.
4) boolean equalsIgnoreCase(String other)
returns true if the string equals other, except for upper/lowercase distinction.
5) int lngth()
returns the length of the string,
6) String replace(char oldChar, char newChar)
Return a new string that is obtained by replacing all characters oldChar in the string with
newChar.
7) String substring(int beginindex)
String substring(int beginlndex, int endlndex)
Return a new string consisting of all characters from begin Index until the end of the string or
until end Index (exclusive).
8) String toLowerCase()
returns a new string containing all characters in the original string, with uppercase characters
converted to lower case.
9) String toUpperCase()
returns a new string containing all characters in the original string, with lowercase characters
converted to upper case.
10) String trim()
returns a new string by eliminating all leading and trailing spaces in the original string.
*****