SlideShare a Scribd company logo
Control statements
Introduction to classes
Closure look at classes and methods
Control
statements
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?
 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
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
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
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
}
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
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
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
// 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
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.
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
Introducing
classes
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}
Class name
convention
class name should be nouns ,
in mixed cases with the first
letter of each internal word
capitalized.
Ex : class CamelCase{
}
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.
instance
variable
example
Public class vehicle
{
//instance variables of class
private int doors;
private int speed;
private string color;
}
simple
class
class box{
int width;
int height;
int depth;
}
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);
}
}
Declaring
objects
Box mybox=new Box();
This statement has 2 steps:
1. Box mybox;
//mybox is reference variable
1. mybox= new Box();
//allocate a box object
Assigning
object
reference
variable
Box b1=new box();
Box b2=b1;
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
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()
}
General form of
methods
type-name(parameters list)
{
//body of the method
}
Ex: void voulme()
{
System.out.println(“volume is”);
System.out.println(width*height*
depth);
]
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.
Rules for
creating
constructors
1. Constructor name must be
same as it class name
2. Constructor must have no
explicit return type.
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(); } }
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();} }
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
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;
}
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();} }
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.
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();
 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
Ppt on java basics

More Related Content

PPTX
Ppt on java basics1
PDF
Java Programming - 03 java control flow
PPTX
Lecture - 3 Variables-data type_operators_oops concept
PDF
Java conditional statements
PPTX
Std 12 computer java basics part 3 control structure
PPTX
Lecture 6 inheritance
PPTX
Lecture 4_Java Method-constructor_imp_keywords
PPTX
Lecture - 5 Control Statement
Ppt on java basics1
Java Programming - 03 java control flow
Lecture - 3 Variables-data type_operators_oops concept
Java conditional statements
Std 12 computer java basics part 3 control structure
Lecture 6 inheritance
Lecture 4_Java Method-constructor_imp_keywords
Lecture - 5 Control Statement

What's hot (20)

PDF
Data structure scope of variables
PDF
Algorithm and Programming (Looping Structure)
PPTX
Lecture 8 abstract class and interface
PDF
Algorithm and Programming (Branching Structure)
PDF
Notes for GNU Octave - Numerical Programming - for Students - 02 of 02 by aru...
KEY
What's New In Python 2.6
PPTX
Looping statements
PPT
Mesics lecture 6 control statement = if -else if__else
PPTX
Control Statement programming
PDF
Lecture 3 Conditionals, expressions and Variables
PPTX
Control structures in java
PPTX
Variable and constants in Vb.NET
PPTX
Virtual function
PDF
Concurrent Stacks and Elimination : The Art of Multiprocessor Programming : N...
PPTX
Decision Making Statement in C ppt
PPTX
Java Decision Control
PDF
maXbox Starter 31 Closures
PPTX
CONTROL STMTS.pptx
PDF
Java Programming - 04 object oriented in java
Data structure scope of variables
Algorithm and Programming (Looping Structure)
Lecture 8 abstract class and interface
Algorithm and Programming (Branching Structure)
Notes for GNU Octave - Numerical Programming - for Students - 02 of 02 by aru...
What's New In Python 2.6
Looping statements
Mesics lecture 6 control statement = if -else if__else
Control Statement programming
Lecture 3 Conditionals, expressions and Variables
Control structures in java
Variable and constants in Vb.NET
Virtual function
Concurrent Stacks and Elimination : The Art of Multiprocessor Programming : N...
Decision Making Statement in C ppt
Java Decision Control
maXbox Starter 31 Closures
CONTROL STMTS.pptx
Java Programming - 04 object oriented in java
Ad

Viewers also liked (20)

PPT
Java lec constructors
PDF
Java ppt Gandhi Ravi ([email protected])
PPT
java Oops.ppt
PPTX
Oop design principles
PDF
Chapter1 Introduction to OOP (Java)
PPTX
Object oriented programming Fundamental Concepts
PPTX
Object Oriented Concept
PPTX
Basics of java 2
PDF
Introduction to basics of java
PPT
Java Basics
PDF
Java Course 2: Basics
PPT
Java Basics
PPT
Programming with Java: the Basics
PDF
Java basics notes
PPT
PALASH SL GUPTA
PDF
Java Course 3: OOP
PPT
Java basics
PPT
Java Programming for Designers
PPTX
Basics of file handling
PPT
2. Basics of Java
Java lec constructors
Java ppt Gandhi Ravi ([email protected])
java Oops.ppt
Oop design principles
Chapter1 Introduction to OOP (Java)
Object oriented programming Fundamental Concepts
Object Oriented Concept
Basics of java 2
Introduction to basics of java
Java Basics
Java Course 2: Basics
Java Basics
Programming with Java: the Basics
Java basics notes
PALASH SL GUPTA
Java Course 3: OOP
Java basics
Java Programming for Designers
Basics of file handling
2. Basics of Java
Ad

Similar to Ppt on java basics (20)

DOCX
Keyword of java
PPTX
Inheritance
PPTX
Polymorphism in C# Function overloading in C#
PPTX
EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...
PPTX
MODULE_3_Methods and Classes Overloading.pptx
PPS
Programming in Arduino (Part 2)
PPT
Decision making in C(2020-2021) statements
PPSX
Java session4
PDF
Week 8,9,10 of lab of data communication .pdf
PPT
Repetition Structure
PDF
22 scheme OOPs with C++ BCS306B_module2.pdfmodule2.pdf
PPTX
Introduction to c sharp
PPTX
C Programming Unit-2
PDF
1669958779195.pdf
DOCX
Java loops
PPTX
class object.pptx
PPT
Java tutorial for Beginners and Entry Level
PPTX
Flow of control C ++ By TANUJ
PDF
polymorphism for b.tech iii year students
PPT
Chapter 7 - Defining Your Own Classes - Part II
Keyword of java
Inheritance
Polymorphism in C# Function overloading in C#
EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...
MODULE_3_Methods and Classes Overloading.pptx
Programming in Arduino (Part 2)
Decision making in C(2020-2021) statements
Java session4
Week 8,9,10 of lab of data communication .pdf
Repetition Structure
22 scheme OOPs with C++ BCS306B_module2.pdfmodule2.pdf
Introduction to c sharp
C Programming Unit-2
1669958779195.pdf
Java loops
class object.pptx
Java tutorial for Beginners and Entry Level
Flow of control C ++ By TANUJ
polymorphism for b.tech iii year students
Chapter 7 - Defining Your Own Classes - Part II

Recently uploaded (20)

PDF
gpt5_lecture_notes_comprehensive_20250812015547.pdf
PDF
Spectral efficient network and resource selection model in 5G networks
PPTX
SOPHOS-XG Firewall Administrator PPT.pptx
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Approach and Philosophy of On baking technology
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Encapsulation theory and applications.pdf
PPTX
A Presentation on Artificial Intelligence
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Accuracy of neural networks in brain wave diagnosis of schizophrenia
PPTX
1. Introduction to Computer Programming.pptx
gpt5_lecture_notes_comprehensive_20250812015547.pdf
Spectral efficient network and resource selection model in 5G networks
SOPHOS-XG Firewall Administrator PPT.pptx
“AI and Expert System Decision Support & Business Intelligence Systems”
Network Security Unit 5.pdf for BCA BBA.
Building Integrated photovoltaic BIPV_UPV.pdf
Diabetes mellitus diagnosis method based random forest with bat algorithm
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Per capita expenditure prediction using model stacking based on satellite ima...
Digital-Transformation-Roadmap-for-Companies.pptx
Approach and Philosophy of On baking technology
Reach Out and Touch Someone: Haptics and Empathic Computing
Encapsulation theory and applications.pdf
A Presentation on Artificial Intelligence
Unlocking AI with Model Context Protocol (MCP)
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Advanced methodologies resolving dimensionality complications for autism neur...
Accuracy of neural networks in brain wave diagnosis of schizophrenia
1. Introduction to Computer Programming.pptx

Ppt on java basics

  • 1. Control statements Introduction to classes Closure look at classes and methods
  • 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.
  • 18. instance variable example Public class vehicle { //instance variables of class private int doors; private int speed; private string color; }
  • 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); } }
  • 21. Declaring objects Box mybox=new Box(); This statement has 2 steps: 1. Box mybox; //mybox is reference variable 1. mybox= new Box(); //allocate a box object
  • 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() }
  • 25. General form of methods type-name(parameters list) { //body of the method } Ex: void voulme() { System.out.println(“volume is”); System.out.println(width*height* depth); ]
  • 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.
  • 27. Rules for creating constructors 1. Constructor name must be same as it class name 2. Constructor must have no explicit return type.
  • 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