Professional Documents
Culture Documents
Over time new enhanced versions of Java have been released. The current version
of Java is Java 1.8 which is also known as Java 8.
Platform independent: Java programs use the Java virtual machine as abstraction
and do not access the operating system directly. This makes Java programs
highly portable. A Java program (which is standard-compliant and follows
certain rules) can run unmodified on all supported platforms, e.g., Windows or
Linux.
Object-orientated programming language: Except the primitive data types, all
elements in Java are objects.
Strongly-typed programming language: Java is strongly-typed, e.g., the types of
the used variables must be pre-defined and conversion to other objects is
relatively strict, e.g., must be done in most cases by the programmer.
Interpreted and compiled language: Java source code is transferred into the
bytecode format which does not depend on the target platform. These bytecode
instructions will be interpreted by the Java Virtual machine (JVM). The JVM
contains a so called Hotspot-Compiler which translates performance critical
bytecode instructions into native code instructions.
Automatic memory management: Java manages the memory allocation and de-
allocation for creating new objects. The program does not have direct access to
the memory. The so-called garbage collector automatically deletes objects to
which no active pointer exists.
Java terminology
Before we start learning Java, lets get familiar with common java terms.
This is generally referred as JVM. Before, we discuss about JVM lets see the
phases of program execution. Phases are as follows: we write the program, then we
compile the program and at last we run the program.
1) Writing of the program is of course done by java programmer like you and me.
2) Compilation of program is done by javac compiler, javac is the primary java
compiler included in java development kit (JDK). It takes java program as input
and generates java bytecode as output.
3) In third phase, JVM executes the bytecode generated by compiler. This is called
program run phase.
So, now that we understood that the primary function of JVM is to execute the
bytecode produced by compiler. Each operating system has different JVM,
however the output they produce after execution of bytecode is same across all
operating systems. That is why we call java as platform independent language.
bytecode
As discussed above, javac compiler of JDK compiles the java source code into
bytecode so that it can be executed by JVM. The bytecode is saved in a .class file
by compiler.
While explaining JVM and bytecode, I have used the term JDK. Let’s discuss
about it. As the name suggests this is complete java development kit that includes
JRE (Java Runtime Environment), compilers and various tools like JavaDoc, Java
debugger,etc.
In order to create, compile and run Java program you would need JDK installed on
your computer.
Java Runtime Environment(JRE)
JRE is a part of JDK which means that JDK includes JRE. When you have JRE
installed on your system, you can run a java program however you won’t be able to
compile it. JRE includes JVM, browser plugins and applets support. When you
only need to run a java program on your computer, you would only need JRE.
The JRE consists of the JVM and the Java class libraries. Those contain the
necessary functionality to start Java programs.
The JDK additionally contains the development tools necessary to create Java
programs. The JDK therefore consists of a Java compiler, the Java virtual machine
and the Java class libraries.
Where it is used?
According to Sun, 3 billion devices run java. There are many devices where java is
currently used. Some of them are as follows:
2) Web Application
An application that runs on the server side and creates dynamic page, is called web
application. Currently, servlet, jsp, struts, jsf etc. technologies are used for creating
web applications in java.
3) Enterprise Application
4) Mobile Application
An application that is created for mobile devices. Currently Android and Java ME
are used for creating mobile applications.
There are many java versions that has been released. Current stable release of Java
is Java SE 8.
Mainly used for C++ is mainly Java is mainly used for application programming. It is
used for system widely used in window, web-based, enterprise and
programming. mobile applications.
Pointers C++ supports Java supports pointer internally. But you can't write
pointers. You the pointer program in java. It means java has
can write pointer restricted pointer support in java.
program in C++.
Compiler and C++ uses Java uses compiler and interpreter both.
Interpreter compiler only.
Call by Value C++ supports Java supports call by value only. There is no call by
and Call by both call by reference in java.
reference value and call by
reference.
Structure and C++ supports Java doesn't support structures and unions.
Union structures and
unions.
Documentation C++ doesn't Java supports documentation comment (/** ... */) to
comment support create documentation for java source code.
documentation
comment.
Virtual C++ supports Java has no virtual keyword. We can override all non-
Keyword virtual keyword static methods by default. In other words, non-static
so that we can methods are virtual by default.
decide whether
or not override a
function.
unsigned right C++ doesn't Java supports unsigned right shift >>> operator that
shift >>> support >>> fills zero at the top for the negative numbers. For
operator. positive numbers, it works same like >> operator.
Inheritance Tree C++ creates a Java uses single inheritance tree always because all
new inheritance classes are the child of Object class in java. Object
tree always. class is the root of inheritance tree in java.
class FirstProgram
{
public static void main(String args[])
{
System.out.println("Hai, welcome");
}
}
Save the File name as same as the Class name
FirstProgram.java
Compile and Run the Program shown below:
Variables in Java
To declare a variable follow this syntax:
data_type variable_name=value;
int i=10;
Similarly we can assign the values to the variables while declaring them, like this:
char ch = 'A';
int number = 100;
or we can do it like this:
char ch;
int number;
...
ch = 'A';
number = 100;
Instance variable
Each instance(objects) of class has its own copy of instance variable. Unlike static
variable, instance variables have their own separate copy of instance variable. We
have changed the instance variable value using object obj2 in the following
program and when we displayed the variable using all three objects, only the obj2
value got changed, others remain unchanged. This shows that they have their own
copy of instance variable.
System.out.println(obj.myInstanceVar);
System.out.println(obj2.myInstanceVar);
System.out.println(obj3.myInstanceVar);
}
}
Local Variable
These variables are declared inside method of the class. Their scope is limited to
the method which means that You can’t change their values and access them
outside of the method.
In this example, I have declared the instance variable with the same name as local
variable, this is to demonstrate the scope of local variables.
byte 0 1 byte
short 0 2 byte
int 0 4 byte
long 0L 8 byte
byte:
This can hold whole number between -128 and 127. Mostly used to save memory
and when you are certain that the numbers would be in the limit specified by byte
data type.
Default size of this data type: 1 byte.
Default value: 0
Example:
class JavaExample {
public static void main(String[] args) {
byte num;
num = 113;
System.out.println(num);
}
}
Try the same program by assigning value assigning 150 value to variable num, you
would get type mismatch error because the value 150 is out of the range of byte
data type. The range of byte as I mentioned above is -128 to 127.
short:
This is greater than byte in terms of size and less than integer. Its range is -32,768
to 32767.
Default size of this data type: 2 byte
int: Used when short is not large enough to hold the number, it has a wider range: -
2,147,483,648 to 2,147,483,647
Default size: 4 byte
Default value: 0
Example:
class JavaExample {
public static void main(String[] args) {
short num;
num = 150;
System.out.println(num);
}
}
The byte data type couldn’t hold the value 150 but a short data type can because it
has a wider range.
long:
Used when int is not large enough to hold the value, it has wider range than int
data type, ranging from -9,223,372,036,854,775,808 to
9,223,372,036,854,775,807.
size: 8 bytes
Default value: 0
Example:
class JavaExample {
public static void main(String[] args) {
long num = -12332252626L;
System.out.println(num);
}
}
double: Sufficient for holding 15 decimal digits
size: 8 bytes
Example:
class JavaExample {
public static void main(String[] args) {
double num = -42937737.9d;
System.out.println(num);
}
}
class JavaExample {
public static void main(String[] args) {
float num = 19.98f;
System.out.println(num);
}
}
boolean: holds either true of false.
class JavaExample {
public static void main(String[] args) {
boolean b = false;
System.out.println(b);
}
}
char: holds characters.
size: 2 bytes
class JavaExample {
public static void main(String[] args) {
char ch = 'Z';
System.out.println(ch);
}
}
Literals in Java
A literal is a fixed value that we assign to a variable in a Program.
int num=10;
Here value 10 is a Integer literal.
char ch = 'A';
Here A is a char literal
Integer Literal
Integer literals are assigned to the variables of data type byte, short, int and long.
byte b = 100;
short s = 200;
int num = 13313131;
long l = 928389283L;
Float Literals
Used for data type float and double.
char ch = 'Z';
String str = "BeginnersBook";
Operators in Java
Types of Operator in Java
1)Basic Arithmetic Operators
2)Assignment Operators
3)Auto-increment and Auto-decrement Operators
4)Logical Operators
5)Comparison(relational) operators
6)Bitwise Operators
7) Ternary Operator
System.out.println("a + b = " + (a + b) );
System.out.println("a - b = " + (a - b) );
System.out.println("a * b = " + (a * b) );
System.out.println("b / a = " + (b / a) );
System.out.println("b % a = " + (b % a) );
System.out.println("c % a = " + (c % a) );
System.out.println("a++ = " + (a++) );
System.out.println("b-- = " + (a--) );
== (equal to) Checks if the values of two operands are equal or not, (A == B) is
if yes then condition becomes true. not true.
System.out.println("a == b = " + (a == b) );
System.out.println("a != b = " + (a != b) );
System.out.println("a > b = " + (a > b) );
System.out.println("a < b = " + (a < b) );
System.out.println("b >= a = " + (b >= a) );
System.out.println("b <= a = " + (b <= a) );
}
}
The Bitwise Operators
Java defines several bitwise operators, which can be applied to the integer types,
long, int, short, char, and byte.
Bitwise operator works on bits and performs bit-by-bit operation. Assume if a =
60 and b = 13; now in binary format they will be as follows −
a = 0011 1100
b = 0000 1101
-----------------
a&b = 0000 1100
a|b = 0011 1101
a^b = 0011 0001
~a = 1100 0011
The following table lists the bitwise operators −
Assume integer variable A holds 60 and variable B holds 13 then −
& (bitwise Binary AND Operator copies a bit to the result (A & B) will give 12 which is
and) if it exists in both operands. 0000 1100
c = a | b; /* 61 = 0011 1101 */
System.out.println("a | b = " + c );
c = a ^ b; /* 49 = 0011 0001 */
System.out.println("a ^ b = " + c );
c = a >> 2; /* 15 = 1111 */
System.out.println("a >> 2 = " + c );
/= Divide AND assignment operator. It divides left operand with the right C /= A is
operand and assign the result to left operand. equivalent
to C = C /
A
C <<= 2 is
<<= Left shift AND assignment operator. same as C
= C << 2
C >>= 2 is
>>= Right shift AND assignment operator. same as C
= C >> 2
C &= 2 is
&= Bitwise AND assignment operator. same as C
=C&2
Miscellaneous Operators
There are few other operators supported by Java Language.
Conditional Operator ( ? : )
Conditional operator is also known as the ternary operator. This operator
consists of three operands and is used to evaluate Boolean expressions. The goal
of the operator is to decide, which value should be assigned to the variable. The
operator is written as −
variable x = (expression) ? value if true : value if false
Following is an example −
Example
public class Test {
public static void main(String args[]) {
int a, b;
a = 10;
b = (a == 1) ? 20: 30;
System.out.println( "Value of b is : " + b );
instanceof Operator
This operator is used only for object reference variables. The operator checks
whether the object is of a particular type (class type or interface type). instanceof
operator is written as −
( Object reference variable ) instanceof (class/interface type)
If the object referred by the variable on the left side of the operator passes the IS-
A check for the class/interface type on the right side, then the result will be true.
Following is an example –
Example
public class Test {
public static void main(String args[]) {
String name = "James";
Example:
class Vehicle {}