Java Unit 2
Java Unit 2
Java Variables
A variable declared inside the class but outside the body of the
method, is called instance variable. It is not declared as static.
It is called instance variable because its value is instance specific
and is not shared among instances.
Static Variable
Data types specify the different sizes and values that can be stored
in the variable. There are two types of data types in Java:
Primitive data types: The primitive data types include Boolean,
char, byte, short, int, long, float and double.
Non-primitive data types: The non-primitive data types
include Classes, Interfaces, and Arrays.
Data Types in Java
Datatypes
Primitive Non-primitive
Floating
char Integer
point
equality == !=
Bitwise bitwise AND &
bitwise exclusive OR ^
bitwise inclusive OR |
Logical logical AND &&
logical OR ||
Ternary ternary ?:
Assignment assignment = += -= *= /= %= &= ^= |= <<=
>>= >>>=
Keywords
1. Object
2. Class
3. Inheritance
4. Polymorphism
5. Abstraction
6. Encapsulation
OBJECTS
The binding (or wrapping up of data) code and data together into
a single unit are known as encapsulation. For example, a capsule, it
is wrapped with different medicines.
A java class is the example of encapsulation. Java bean is the fully
encapsulated class because all the data members are private here.
OOPs CONCEPTS
Apart from these concepts, there are some other terms which are
used in Object-Oriented design:
Coupling
Cohesion
Association
Aggregation
Composition
Coupling
Association represents the relationship between the objects. Here, one object can be
associated with one object or many objects. There can be four types of association between the
objects:
One to One
One to Many
Many to One, and
Many to Many
Let's understand the relationship with real-time examples. For example, One country can have
one prime minister (one to one), and a prime minister can have many ministers (one to many).
Also, many MP's can have one prime minister (many to one), and many ministers can have
many departments (many to many).
Association can be undirectional or bidirectional
Aggregation
Control Statements
Selection
Iteration
Jump
27
Selection Statements
Selection
Selection Statements
Switch Statement
28
if Statements
if Statements
Simple if
if else
Nested if
29
Simple if
Syntax :
if (condition)
{
statement1;
}
Purpose: The statements will be evaluated if the value of the condition is true.
30
Simple if
True False
Condition
Statements
End
31
Example
32
if else
Syntax :
if (condition)
{
statement1;
}
else
{
statement2;
}
Purpose: The statement 1 is evaluated if the value of the condition is true otherwise
statement 2 is true.
33
if else
Flow Chart: Start
True False
Condition
Statement 1 Statement 2
End
34
Example
35
If-else-if Ladder
Syntax :
if(condition)
statements;
else if(condition)
statements;
else if(condition)
statements;
...
...
else
statements;
36
Examples
import java.util.Scanner;
class Day else if (day == 3)
{ {
public static void main(String args[]) System.out.println("\n Wednesday");
{ }
Scanner s = new Scanner(System.in); else if (day == 4)
System.out.println("Enet day between 0 to 6 Day = "); {
int day = s.nextInt(); System.out.println("\n Thursday");
if (day == 0) }
{ else if (day == 5)
System.out.println("\n Sunday"); {
} System.out.println("\n Friday");
else if (day == 1) }
{ else
System.out.println("\n Monday"); {
} System.out.println("\n Saturday");
else if (day == 2) }
{ }
System.out.println("\n Tuesday"); }
}
37
Nested if
• A nested if is an if statement that is the target of another if or else.
• Nested ifs are very common in programming.
Syntax :
if(condition)
{
if(condition)
statements....
else
statements....
}
else
{
if(condition)
statements....
else
statements....
}
38
Example
39
switch
Syntax :
switch (expression)
{
case value 1 :
statement 1 ; break;
case value 2 :
statement 2 ; break;
...
...
case value N :
statement N ; break;
default :
statements ; break;
}
Purpose: The statements N will be evaluated if the value of the logical expression is true.
40
switch
Start
Flow Chart:
Variable or Expression
Case A Statements
Case A
break;
True
False
… Case C Statements
True break;
False
End
41
Example
42
Iteration Statements
Iterations/ Loops
Each loop has four types of
statements :
while
Initialization
Condition checking
Execution
Increment / Decrement do while
for
43
while
Syntax:
m=1
initialization while(m<=20)
while(final value) {
{ System.out.println(m);
statements; m=m+1;
increment/decrement; }
}
Purpose: To evaluate the statements from initial value to final value with given
increment/decrement.
44
Example
print values from 1 to 10
45
do while
Syntax:
initialization m=1
do do
{ {
statements; System.out.println(m);
increment/decrement; m=m+1;
} }
while(final value); while(m==20);
Purpose: To evaluate the statements from initial value to final value with given
increment/decrement.
46
Example
class dowhile1
{
public static void main(String args[])
{
int i = 1;
int sum = 0;
do
{
sum = sum + i;
i++;
}while (i<=10);
System.out.println("\n\n\tThe sum of 1 to 10 is .. " + sum);
}
}
Output :
The sum of 1 to 10 is .. 55 22
for
Syntax:
Purpose: To evaluate the statements from initial value to final value with given
increment/decrement.
48
Example
class for1
{
public static void main(String args[])
{
int i;
for (i=0;i<5;i++)
{
System.out.println("\nExample of for loop ");
}
}
Output :
Example of for loop
Example of for loop
Example of for loop
Example of for loop
Example of for loop
49
Jump Statements
Jump
break
continue
return
50
The break statement
statementName : SomeJavaStatement
break statementName;
51
Example
class break1 Output :
{ 1
public static void main(String args[]) 2
{ 3
int i = 1; 4
while (i<=10)
{
System.out.println("\n" + i);
i++;
if (i==5)
{
break;
}
}
}
}
52
continue Statement
The remaining statements in the loop are skipped. The execution starts from the
53
Example
class continue1 Output :
{ 1
public static void main(String args[]) 3
{ 5
for (int i=1; i<1=0; i++) 7
{ 9
if (i%2 == 0)
continue;
System.out.println("\n" + i);
}
}
}
54
The return Statement
55
Example
class Return1
{
public static void main(String args[])
{
boolean t = true; Output :
System.out.println("Before the return."); Before the return.
if(t)
return; // return to caller
System.out.println("This won't execute.");
}
}
56