3. Any language uses control
statements to cause the flow of
execution to advance and
branch based on the state of
the program.
Why do we
need control
statements?
4. Selection : to choose
different paths of execution
based on outcome or state
of the variable.
Iterative : to repeat one or
more statements.
Jump : to execute in non-
linear fashion.
Control
Statements
categories
5. If statement is conditional branch
statement.
If Syntax:
if(condition)statement1;
else statement2;
Ex : if(a>b)a=0;
else b=0;
Java’s
selection
statements
oIf
oswitch
6. Switch is multi-way branch
statement.
Syntax : switch(expression){
Case value1:
//statement sequence;
break;
Case value2:
//statement sequence;
break;
default:
//default statement sequence}
Switch
case
7. Java’s iterative
statements
o while
o do-while
o for
Iterative statements create what
we commonly call loops which
repeatedly executes the same set
of instructions until a termination
condition is met.
while syntax:
While(condition){
//body of loop
}
8. If the conditional expression controlling
a while loop is initially false , then the
body of the loop will not be executed at
all.
Sometimes it is desirable to execute the
body of loop at least once even if the
conditional expression is false.
Condition is checked at the end of the
loop.
syntax:
do{
//body of loop
}while(condition);
do- while
case
9. Syntax:
for(initialization ; condition ; iteration)
{ //body of loop
}
When loop starts the initialization
portion of the loop executes . Here
initialization expression is executed only
once.
Next condition is evaluated.
If the condition is true the body of the
loop is executed. If false then loop
terminates.
Then iteration is executed i . e
increments or decrements the loop
control variable
for loop
case
10. Used to transfer control to
another part of your program
Break statement has 3 uses:
1. Terminates a statement
sequence in switch
statement
2. To exit a loop
3. Civilized form of goto
Java’s jump
statements
o break
o continue
o return
11. // Using break as a civilized form of goto.
class Break {
public static void main(String args[]) {
boolean t = true;
first: {
second: {
third: {
System.out.println("Before the break.");
if(t) break second; // break out of second block
System.out.println("This won't execute");
}
System.out.println("This won't execute");
}
System.out.println("This is after second block.");
}
}
}
break as
civilized form
of goto
12. continue
jump
statement
Skips the current iteration of a
for , while or do-while loop.
The unlabeled form skips to
the end of the innermost loop’s
body and evaluates Boolean
expression that controls the
loop.
break leaves the loop.
continue jumps to the next
iteration.
13. The return statement is used
to explicitly return from a
method. That is, it causes
program control to transfer back
to the caller of the method.
// Demonstrate return.
class Return {
public static void main(String args[]) {
boolean t = true;
System.out.println("Before the return.");
if(t) return; // return to caller
System.out.println("This won't execute.");
}
}
The output from this program is shown here:
Before the return.
Return jump
statement
15. Class
fundamentals :
General form
of class
Class syntax:
Class classname{
type instancevariable1;
type instancevariable2;
Type methodname1(parameter
list){//body of method}
Type methodname2(parameter
list){//body of method}
16. Class name
convention
class name should be nouns ,
in mixed cases with the first
letter of each internal word
capitalized.
Ex : class CamelCase{
}
17. what is
instance
variable?
Instance variables are declared in
a class, but outside a method,
constructor or any block.
When a space is allocated for an
object in the heap, a slot for each
instance variable value is created.
Instance variables are created
when an object is created with the
use of the keyword 'new' and
destroyed when the object is
destroyed.
20. Declaring
objects
class Box {
int width;
int height;
int depth;
}
// This class declares an object of type Box.
class BoxDemo {
public static void main(String args[]) {
Box mybox = new Box();
int vol;
// assign values to mybox's instance variables
mybox.width = 10;
mybox.height = 20;
mybox.depth = 15;
// compute volume of box
vol = mybox.width * mybox.height * mybox.depth;
System.out.println("Volume is " + vol);
}
}
23. Introducing
methods
Why use
methods?
Methods are how we
communicate with objects.
When we call or invoke a method
we are asking the objects to carry
out a task
For reusable code
To simplify
For top-down programming
To create conceptual units
To parameterize code
24. Method name
convention
Method name should be
verbs , in mixed case with the
first letter lowercase , with the
first letter of each internal
word capitalized.
Ex : class CamelCase{
Void runFast()
}
26. Constructor and
its purpose
Constructor is a special type of
method that is used to initialize
the object.
It is invoked at the time of
object creation.
It constructs the values i.e
provides data for the object that
is why it is known as
constructor.
28. Types of
Constructors
1. Default constructor(no-arg
consuctor)
2. Parameterized constructor.
A constructor that has no
parameters is known as default
constructor.
Class bike(){
Bike(){system.out.println(“bike
constructor”);}
Public static void main(){
bike b=new bike(); } }
29. Parameterized
constructors
A constructor that has parameters is
known as parameterized constructor.
Class student(){
Int id;
String name
student(int i, string n) { id=i; name=n;
}
Void display(){ system.out.println(id+“
”+name);}
Public static void main(){
student s=new student(1,”karan”);
s.display();} }
30. this keyword
this is a reference variable that refers to the
current object.
this can be used to refer current class
instance variable.
this can be used to invoke current class
constructor
Used to invoke current class method
(implicitly)
this can be passed as an argument in the
constructor call
this can be passed as an argument in the
method call.
this can also be used to return the current
class instance
31. Class student(){
Int id;
String name;
student(int id, string name) { id=id;
name=name; }
Void display(){
system.out.println(id+“ ”+name);}
Public static void main(){
student s=new student(1,”karan”);
s.display();} }
In this example , parameter and
instance variables are same that is
why we are using this keyword to
distinguish between local and
instance variable.
student(int id, string name)
{
this.id=id;
this.name=name;
}
32. this keyword used
for current class
instance variable
and constructor
Class student(){
Int id;
String name;
Student();{system.out.println(“default”)
;}
student(int id, string name) {
this();
this(id, name);
this.id=id; this.name=name; }
Void display(){ system.out.println(id+“
”+name);}
Public static void main(){
student s=new student(1,”karan”);
s.display();} }
33. Garbage
collection and its
advantages
Garbage collection is a process of
reclaiming the runtime unused memory
automatically i.e destroying unused
objects.
It makes java memory efficient
because it removes the unreferenced
objects from heap.
It is automatically done by the
garbage collector so we don’t need to
make extra efforts.
34. How can objects
be unreferenced?
There are many ways:
By nulling the reference variable
By assigning a reference to another.
By anonymous object etc.
//Nulling the reference
student s=new student();
s=null;
//Assign reference to another
student s1=new student();
student s2=new student();
s1=s2;//s1 is available for gc
//anonymous object
new student();
35. The finalize() method is invoked each
time before the object is garbage
collected.
This method can be used to perform
cleanup processing.
This method is defined in object class
as:
Protected void finalize(){ }
Garbage collector of JVM collects
only those objects created by ‘new’
keyword.so any objects without new
use finalize method to cleanup
processing
finalize( )
method