Java New Material
Java New Material
Real machine
Operating system
Compiler Interpreter
Users
Parts of Java:
: Naming Conventions in Java
[DATA TYPES IN JAVA: [UMP-10M]
Every variable in Java has a data type. Data type specifies the size and type of values that
can be stored. Java language has rich data types, that a variable can store such as integer,
floating, character, String, boolean etc.
DATA TYPES OF JAVA
int double
long
The basic data types are integer-based and floating-point based. Java language supports
both integer and Real or Floating point data types. The memory size of basic data types may
change according to operating system. The following are Primitive or basic data types.
Data Java Data Memory
S.No. Minimum Value Maximum Value
types Types Size
I Integer
a. byte 1 byte −128 127
b. short 2 bytes −32,768 32,767
c. int 4 bytes −2,147,483,648 2,147,483,647
d. long 8 bytes −9,223,372,036,854,775,808 9,223,372,036,854,775,807
Primitive
II Floating point
Data
a. float 4 bytes 3.4e−038 3.4e+038
Types
b. double 8 bytes 1.7e−308 1.7e+308
III Character
a. char 2 bytes −32,768 32,767
b. String
c. boolean 1 byte −128 127
In Java, Non-Primitive or Derived Data types are defined by the programmer or user. The
following are the best derived or user defined data types.
[a. Class:[UM-5M]
Class can be defined as a collection of data members and member functions applied on that
data. It is used to create the require classes for a specific purpose like person, bank etc. It is also
used to defined abstract data types (ADT)
b. Interface:
An interface is a named collection of abstract methods and constants. In the Java
programming language, an interface is a reference type, similar to a class that can contain only
constants, method signatures (prototypes), and nested types. There are no method bodies. Interfaces
cannot be instantiated they can only be implemented by classes or extended by other interfaces.
Interface declarations in Java introduce a new data type. In Java, interfaces are used to implement
multiple-inheritance.]
c. Array:
it is a user defined data type and it can be defined as a collection of similar data items which are
stored in a continuous sequence of memory locations which are represented by a single variable. It is
a static memory allocation. It stores the values in memory at the fixed size]
1. ARITHMETIC OPERATORS:
Multiplication, Divisionand Modulus division etc., Assume variable a holds 10 and variable b holds 20,
then:
S.NO
OPERATOR DESCRIPTION EXAMPLE
.
1. + Addition - Adds values on either side of the operator a + b will give 30
Subtraction - Subtracts right hand operand from left hand
2. - a - b will give -10
operand
3. * Multiplication - Multiplies values on either side of the operator a * b will give 200
4. / Division - Divides left hand operand by right hand operand b / a will give 2
Modulus - Divides left hand operand by right hand operand and
5. % b % a will give 0
returns remainder
These Operators are used to compare two or more Operands or Variables and then produce
either True or False. These Operators are used to build Relational Or Boolean Expressions. Assume
JAVA Language supports the following logical operators – Logical Operators are used to
combine two or more Relational expressions in to a Logical expression. These Logical expressions
produce either True or False. Assume variable A holds 10 and B holds 20, then –
S.NO OPERATOR AND DESCRIPTION
&& (Logical AND) If both the operands are non-zero, then the condition becomes true.
1
Ex: (A && B) is true.
|| (Logical OR) If any of the two operands are non-zero, then the condition becomes true.
2
Ex: (A || B) is true.
! (Logical NOT) Reverses the logical state of its operand. If a condition is true, then the Logical NOT
3
operator will make it false. Ex: !(A && B) is false.
4. ASSIGNMENT OPERATORS:
+= (Add and Assignment) It adds the right operand to the left operand and assigns the result to the
2
left operand. Ex: C += A is equivalent to C = C + A
−= (Subtract and Assignment)It subtracts the right operand from the left operand and assigns the
3
result to the left operand. Ex: C -= A is equivalent to C=C–A
*= (Multiply and Assignment) It multiplies the right operand with the left operand and assigns the
4
result to the left operand. Ex: C *= A is equivalent to C = C * A
/= (Divide and Assignment) It divides the left operand with the right operand and assigns the result to
5
the left operand. Ex: C /= A is equivalent to C = C / A
%= (Modules and Assignment) It takes modulus using two operands and assigns the result to the left
6
operand. Ex: C %= A is equivalent to C = C % A
In Pre-Increment Operation, “++” is written before Variable name. Value is Incremented First
and then incremented value is used in expression. “++” cannot be used over “ constant” of “final
Variable“.During Expression evaluation only pre incremented value is used. The syntax of Pre-
Increment Operator is
Syntax: <++ Operator> <Variable name>;
Example: int i=1; ++i;
b. Post Increment Operator:
In Post-Increment Operation, “++” is written after Variable name, First value is used in
expression then Value is Incremented. It is used just after completion of expression evaluation. The
syntax of Post-Increment Operator is
Syntax: <Variable name> <++ Operator>;
Example: int i=1; i++;
2. DECREMENT OPERATOR( -- ):
In Pre-Decrement Operation, “--” is written before Variable name. Value is decremented First
and then decremented value is used in expression. “--” cannot be used over “c onstant” of “final
Variable“. During Expression evaluation only Pre decremented value is used. The syntax of Pre-
decrement Operator is
Syntax: <-- Operator> <Variable name>;
Example: int i=1; --i;
b. Post Decrement Operator:
In Post-Decrement Operation, “--” is written after Variable name, First value is used in
expression then Value is decremented. It is used just after completion of expression evaluation. The
syntax of Post-decrement Operator is
Syntax: <Variable name> <-- Operator>;
Example: int i=1; i--;
conditional operator ? : which can be used to replace if...else statements. It has the following
general form
Syntax: Numeric_Variable=(Test-Condition) ? (const1/Variable1/Exp1) : (const2/Variable2/Exp2);
Where const1,2 or Variable1,2 or Exp1,2 Numeric values. In the above syntax If Test condition
is true then const1/Variable1/Exp1 is evaluated and then result is assigned to Numeric_Variable
otherwise const2/Variable2/Exp2 is evaluated and then result is assigned to Numeric_Variable.
Example: K=(A>B && A>C) ? A : B;
7. BITWISE OPERATORS:
Bitwise Operators are used to perform operations on Bits and Bytes, these operations are
called Boolean Operations. JAVA Language supports the following Bitwise Operators −Assume variable
A holds 2 and B holds 3, then –
S.NO OPERATOR AND DESCRIPTION
1 & (Bitwise AND) It performs a Boolean AND operation on each bit of its integer arguments. Ex: (A & B) is 2.
2 | (BitWise OR) It performs a Boolean OR operation on each bit of its integer arguments. Ex: (A | B) is 3.
^ (Bitwise XOR) It performs a Boolean exclusive OR operation on each bit of its integer arguments. Exclusive OR
3
means that either operand one is true or operand two is true, but not both. Ex: (A ^ B) is 1.
4 ~ (Bitwise Not) It is a unary operator and operates by reversing all the bits in the operand. Ex: (~B) is -4.
<< (Left Shift) It moves all the bits in its first operand to the left by the number of places specified in the second
5 operand. New bits are filled with zeros. Shifting a value left by one position is equivalent to multiplying it by 2,
shifting two positions is equivalent to multiplying by 4, and so on. Ex: (A << 1) is 4.
<< (Left Shift) It moves all the bits in its first operand to the left by the number of places specified in the second
5 operand. New bits are filled with zeros. Shifting a value left by one position is equivalent to multiplying it by 2,
shifting two positions is equivalent to multiplying by 4, and so on. Ex: (A << 1) is 4.
>> (Right Shift) Binary Right Shift Operator. The left operand’s value is moved right by the number of bits specified
6
by the right operand. Ex: (A >> 1) is 1.
>>> (Right shift with Zero) This operator is just like the >> operator, except that the bits shifted in on the left
7
are always zero. Ex: (A >>> 1) is 1.
8. SPECIAL OPERATORS:
The instanceof is an object reference operator and returns true if the object on the left hand
side is an instance of the class given on the right hand side. This Operator determines whether the
belongs to a particular class or not. The syntax is
Syntax: <Object Name> instanceof <Class Name>;
Example: obj1 instanceof Parent;
class Parent{}
class instanceof Demo
{
public static void main(String[] args)
{
Parent obj1 = new Parent();
System.out.println("obj1 instanceof Parent: " + (obj1 instanceof Parent));
}
}
Output: obj1 instanceof Parent: true
The dot operator “ . ” or selection operator is used to access the instance variables and
Java Language supports Decision Making and Branching Statements which are used to perform
different actions based on different conditions. Java Language supports the following forms.
If statements:
The if statement in JAVA language is used to perform operation on the basis of condition.
These statements can perform operations either condition is true or false. There are many ways to use
if statement in JAVA language:
1. Simple if statement
2. if...else statement
3. if...else if... statement.(Nested if )
4. Conditional or Terinory Operator
5. switch-case and default statement
1. Simple if statement:
The simple if statement in Java language is used to execute the code if condition is true. The
syntax of if statement is given below:
Syntax: if(Test-Condition)
{
Single Statement; or Group of Statements; //code to be executed
}
Example: class jack
{
public static void main(String args[])
{
int n=0;
if(n%2==0)
{
System.out.println("The Even Number is " + n);
}
}
}
. if-else Statement:
The if-else statement in Java language is used to execute the code if condition is true or false.
The syntax of if-else statement is given below:
Syntax: if(Test-Condition)
{
Single Statement; or Group of Statements; // code to be executed if condition is true
}
else
{
Single Statement; or Group of Statements; // code to be executed if condition is false
}
The Nested if ( if else-if ladder ) is used to execute one code from multiple conditions. The
syntax of Nested if ( if else-if ladder )statement is given below:
Syntax:
if(Test-condition1)
{
Statement or Statements block //code to be executed if condition1 is true
}
else
if(Test-condition2)
{
Statement or Statements block //code to be executed if condition2 is true
}
else
if(Test-condition3)
{
Statement or Statements block //code to be executed if condition3 is true
}
---
---
---
else
{
Statement or Statements block //code to be executed if all the conditions are false
}
Example: class jack
{
public static void main(String args[])
{
int a,b,c;
if(a>b && b>c)
System.out.println("The big is " + a);
else
if(b>a && b>c)
System.out.println("The big is " + b);
else
System.out.println("The big is " + b);
}
}
conditional operator ? : which can be used to replace if...else statements. It has the following
syntax
Syntax: Numeric_Variable=(Test-Condition) ? (const1/Variable1/Exp1) : (const2/Variable2/Exp2);
Where const1,2 or Variable1,2 or Exp1,2 Numeric values. In the above syntax If Test condition
is true then const1/Variable1/Exp1 is evaluated and then result is assigned to Numeric_Variable
otherwise const2/Variable2/Exp2 is evaluated and then result is assigned to Numeric_Variable.
Examples: K=(A>B && A>C) ? A : B;
R=(X==Y) ? (X+Y) : (X-Y);
N=(A>B) ? 100 : 200;
5. SWITCH-CASE-DEFAULT STATEMENT:
The switch statement in Java language is used to execute the code from multiple conditions. It
is like Nested if( if else-if ladder) statement. The syntax of switch statement in Java language is given
below:
Syntax:
switch(Variable or Constant or any expression)
{
case value1: Statement or Statements block; //code to be executed;
break; //optional
case value2: Statement or Statements block; //code to be executed;
break; //optional
case value3: Statement or Statements block; //code to be executed;
break; //optional
------------- -------------
------------- -------------
------------- -------------
default: Statement or Statements block;
//
code to be executed if all cases are not matched;
}
Rules for switch statement in Java language:
1. The switch expression must be of integer or character type.
2. The case value must be integer or character constant.
3. The case value can be used only inside the switch statement.
4. The break statement in switch case is not must. It is optional. If there is no break statement found
in switch case, all the cases will be executed after matching the case value.
Example:
class jack
{
public static void main(String args[])
{
char ch=’o’;
switch(ch)
{
case ‘v’ : System.out.println(“ Violet “); break;
case ‘i’ : System.out.println(“ Indigo “); break;
case ‘b’ : System.out.println(“ Blue “); break;
case ‘g’ : System.out.println (“ Green “); break;
case ‘y’ : System.out.println (“ Yellow “); break;
case ‘o’ : System.out.println (“ Orange “); break;
case ‘r’ : System.out.println (“ Red “); break;
default : System.out.println (“ Enter right choice”);
}
}
}
The most basic loop in Java language is the while loop. The purpose of a while loop is to
execute a statement or statement block repeatedly as long as an expression or Test-condition is true.
Once the expression becomes false, the loop terminates. It iterates the code until condition is false.
The while loop checks the Test condition at the beginning of the loop. This means that this
loop will always be executed if the condition is True. So, it is called Entry Controlled Loop.
Syntax: Exp1;
while (Test-condition)
{
Statement or Statements;
----------
---------- //body of the loop code to be executed
Exp2;
}
Where Test-condition is Boolean Expression, Exp1 is initialization expression and Exp2 is
Increment or Decrement expressions. Exp1 and Exp2 are optional.
Example: class jack
{
public static void main(String args[])
{
int i=1;
while(i<=n)
{
System.out.println(i);
i++;
}
}
}
The do...while loop is similar to the while loop except that the condition check happens at the
end of the loop. This means that the loop will always be executed at least once, even if the condition
is false. So, it is called Exit Controlled Loop.
Syntax: Exp1;
do
{
Statement or Statements;
----------
---------- //body of the loop code to be executed
Exp2;
} while (Test-condition);
Where Test-condition is Boolean Expression, Exp1 is initialization expression and Exp2 is
Increment or Decrement expressions. Exp1 and Exp2 are optional
Example: class jack
{
public static void main(String args[])
{
int i=1;
do
{
System.out.println(i);
i++;
}while(i<=n);
}
}
DIFFERENCE BETWEEN ENTRY CONTROLLED AND EXIT CONTROLLED LOOPS:
The following C progrming code shoes the difference between entry controlled and exit
controlled loops
i=6; i=6;
while(i<=5) do
{ {
System.out.println(i); System.out.println(i);
i++; i++;
} } while(i<=5);
This code does not produce the output. This code produces the output
3. FOR LOOP:
The 'for( )' loop is the most compact form of looping. It includes the following three important
parts
1. The loop initialization where we initialize our counter to a starting value. The
initialization statement is executed before the loop begins.
2. The Test Condition which will test if a given condition is true or not. If the condition is true,
then the code given inside the loop will be executed, otherwise the control will come out of the
loop.
3. The Iteration statement (Increment or Decrement) can increase or decrease counter. We can
put all the three parts in a single line separated by semicolons.
Syntax: for (Initialization Statements; Test Condition; Iteration Statements)
{
Statement or Statements;
----------
---------- //body of the loop code to be executed
}
Example: class jack
{
public static void main(String args[])
{
int i=1;
for(i=1;i<=5;i++)
{
System.out.println(i);
}
}
}
A. BREAK STATEMENT:
The break statement in Java language is used to break the execution of loop (while(), do-
while() and for() ) and switch-case-default. In case of inner loops, it terminates the control of inner
loop only. There can be two uses of Java break keyword: is
1. With loop
2. With switch case
1. break Statement with Loop:
Syntax:
The syntax of for loop is Java language is as follows
for (Initialization Statements; Test Condition; Iteration Statements)
{
Statement or Statements;
----------
---------- //body of the loop code to be executed
jumping Statement
break;
}
The break Statement can be write in while() loop, do-while() loop, for() loop and switch-case-
default() statements.
The break statement in switch case is not must. It is optional. If there is no break statement
found in switch case, all the cases will be executed after matching the case value. The syntax of break
statement with switch case default is.
Syntax:
switch(Variable or Constant or any expression)
{
case value1: Statement or Statements block; //code to be executed;
break; //optional
case value2: Statement or Statements block; //code to be executed;
break; //optional
case value3: Statement or Statements block; //code to be executed;
break; //optional
------------- -------------
------------- -------------
default Statement or Statements block;
// code to be executed if all cases are not matched;
}
Example:
class jack
{
public static void main(String args[])
{
char ch=’o’;
switch(ch)
{
case ‘v’ : System.out.println(“ Violet “); break;
case ‘i’ : System.out.println(“ Indigo “); break;
case ‘b’ : System.out.println(“ Blue “); break;
case ‘g’ : System.out.println (“ Green “); break;
case ‘y’ : System.out.println (“ Yellow “); break;
case ‘o’ : System.out.println (“ Orange “); break;
case ‘r’ : System.out.println (“ Red “); break;
default : System.out.println (“ Enter right choice”);
}
}
}
B. JAVA CONTINUE STATEMENT:
The continue statement in Java language is used to continue the execution of loop (while, do
while and for).It is used with if condition within the loop. In case of inner loops, it continues the
control of inner loop only.
Syntax: for (Initialization Statements; Test Condition; Iteration Statements)
{
Statement or Statements;
----------
---------- //body of the loop code to be executed
if( Test Condition )
continue;
----------
}
Example: class jack
{
public static void main(String args[])
{
int i;
for( i=1; i<=5; i++)
{
if (i==3)
continue;
System.out.println(i); }
} ]