SlideShare a Scribd company logo
1 of 36
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

What's hot

Data structure scope of variables
Data structure scope of variablesData structure scope of variables
Data structure scope of variablesSaurav Kumar
 
Algorithm and Programming (Looping Structure)
Algorithm and Programming (Looping Structure)Algorithm and Programming (Looping Structure)
Algorithm and Programming (Looping Structure)Adam Mukharil Bachtiar
 
Lecture 8 abstract class and interface
Lecture   8 abstract class and interfaceLecture   8 abstract class and interface
Lecture 8 abstract class and interfacemanish kumar
 
Algorithm and Programming (Branching Structure)
Algorithm and Programming (Branching Structure)Algorithm and Programming (Branching Structure)
Algorithm and Programming (Branching Structure)Adam Mukharil Bachtiar
 
Notes for GNU Octave - Numerical Programming - for Students - 02 of 02 by aru...
Notes for GNU Octave - Numerical Programming - for Students - 02 of 02 by aru...Notes for GNU Octave - Numerical Programming - for Students - 02 of 02 by aru...
Notes for GNU Octave - Numerical Programming - for Students - 02 of 02 by aru...ssuserd6b1fd
 
What's New In Python 2.6
What's New In Python 2.6What's New In Python 2.6
What's New In Python 2.6Richard Jones
 
Looping statements
Looping statementsLooping statements
Looping statementsJaya Kumari
 
Mesics lecture 6 control statement = if -else if__else
Mesics lecture 6   control statement = if -else if__elseMesics lecture 6   control statement = if -else if__else
Mesics lecture 6 control statement = if -else if__elseeShikshak
 
Lecture 3 Conditionals, expressions and Variables
Lecture 3   Conditionals, expressions and VariablesLecture 3   Conditionals, expressions and Variables
Lecture 3 Conditionals, expressions and VariablesSyed Afaq Shah MACS CP
 
Control structures in java
Control structures in javaControl structures in java
Control structures in javaVINOTH R
 
Variable and constants in Vb.NET
Variable and constants in Vb.NETVariable and constants in Vb.NET
Variable and constants in Vb.NETJaya Kumari
 
Virtual function
Virtual functionVirtual function
Virtual functionzindadili
 
Concurrent Stacks and Elimination : The Art of Multiprocessor Programming : N...
Concurrent Stacks and Elimination : The Art of Multiprocessor Programming : N...Concurrent Stacks and Elimination : The Art of Multiprocessor Programming : N...
Concurrent Stacks and Elimination : The Art of Multiprocessor Programming : N...Subhajit Sahu
 
Decision Making Statement in C ppt
Decision Making Statement in C pptDecision Making Statement in C ppt
Decision Making Statement in C pptMANJUTRIPATHI7
 
Java Decision Control
Java Decision ControlJava Decision Control
Java Decision ControlJayfee Ramos
 
maXbox Starter 31 Closures
maXbox Starter 31 ClosuresmaXbox Starter 31 Closures
maXbox Starter 31 ClosuresMax Kleiner
 

What's hot (20)

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

Viewers also liked

Java ppt Gandhi Ravi (gandhiri@gmail.com)
Java ppt  Gandhi Ravi  (gandhiri@gmail.com)Java ppt  Gandhi Ravi  (gandhiri@gmail.com)
Java ppt Gandhi Ravi (gandhiri@gmail.com)Gandhi Ravi
 
Oop design principles
Oop design principlesOop design principles
Oop design principlesSayed Ahmed
 
Object oriented programming Fundamental Concepts
Object oriented programming Fundamental ConceptsObject oriented programming Fundamental Concepts
Object oriented programming Fundamental ConceptsBharat Kalia
 
Object Oriented Concept
Object Oriented ConceptObject Oriented Concept
Object Oriented ConceptD Nayanathara
 
Basics of java 2
Basics of java 2Basics of java 2
Basics of java 2Raghu nath
 
Introduction to basics of java
Introduction to basics of javaIntroduction to basics of java
Introduction to basics of javavinay arora
 
Java Course 2: Basics
Java Course 2: BasicsJava Course 2: Basics
Java Course 2: BasicsAnton Keks
 
Programming with Java: the Basics
Programming with Java: the BasicsProgramming with Java: the Basics
Programming with Java: the BasicsJussi Pohjolainen
 
Java Course 3: OOP
Java Course 3: OOPJava Course 3: OOP
Java Course 3: OOPAnton Keks
 
Java Programming for Designers
Java Programming for DesignersJava Programming for Designers
Java Programming for DesignersR. Sosa
 
Basics of file handling
Basics of file handlingBasics of file handling
Basics of file handlingpinkpreet_kaur
 

Viewers also liked (20)

Java lec constructors
Java lec constructorsJava lec constructors
Java lec constructors
 
Java ppt Gandhi Ravi (gandhiri@gmail.com)
Java ppt  Gandhi Ravi  (gandhiri@gmail.com)Java ppt  Gandhi Ravi  (gandhiri@gmail.com)
Java ppt Gandhi Ravi (gandhiri@gmail.com)
 
java Oops.ppt
java Oops.pptjava Oops.ppt
java Oops.ppt
 
Oop design principles
Oop design principlesOop design principles
Oop design principles
 
Chapter1 Introduction to OOP (Java)
Chapter1 Introduction to OOP (Java)Chapter1 Introduction to OOP (Java)
Chapter1 Introduction to OOP (Java)
 
Object oriented programming Fundamental Concepts
Object oriented programming Fundamental ConceptsObject oriented programming Fundamental Concepts
Object oriented programming Fundamental Concepts
 
Object Oriented Concept
Object Oriented ConceptObject Oriented Concept
Object Oriented Concept
 
Basics of java 2
Basics of java 2Basics of java 2
Basics of java 2
 
Introduction to basics of java
Introduction to basics of javaIntroduction to basics of java
Introduction to basics of java
 
Java Basics
Java BasicsJava Basics
Java Basics
 
Java Course 2: Basics
Java Course 2: BasicsJava Course 2: Basics
Java Course 2: Basics
 
Java Basics
Java BasicsJava Basics
Java Basics
 
Programming with Java: the Basics
Programming with Java: the BasicsProgramming with Java: the Basics
Programming with Java: the Basics
 
Java basics notes
Java basics notesJava basics notes
Java basics notes
 
PALASH SL GUPTA
PALASH SL GUPTAPALASH SL GUPTA
PALASH SL GUPTA
 
Java Course 3: OOP
Java Course 3: OOPJava Course 3: OOP
Java Course 3: OOP
 
Java basics
Java basicsJava basics
Java basics
 
Java Programming for Designers
Java Programming for DesignersJava Programming for Designers
Java Programming for Designers
 
Basics of file handling
Basics of file handlingBasics of file handling
Basics of file handling
 
2. Basics of Java
2. Basics of Java2. Basics of Java
2. Basics of Java
 

Similar to Ppt on java basics

Keyword of java
Keyword of javaKeyword of java
Keyword of javaJani Harsh
 
Polymorphism in C# Function overloading in C#
Polymorphism in C# Function overloading in C#Polymorphism in C# Function overloading in C#
Polymorphism in C# Function overloading in C#Abid Kohistani
 
EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...
EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...
EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...vekariyakashyap
 
MODULE_3_Methods and Classes Overloading.pptx
MODULE_3_Methods and Classes Overloading.pptxMODULE_3_Methods and Classes Overloading.pptx
MODULE_3_Methods and Classes Overloading.pptxVeerannaKotagi1
 
Repetition Structure
Repetition StructureRepetition Structure
Repetition StructurePRN USM
 
Introduction to c sharp
Introduction to c sharpIntroduction to c sharp
Introduction to c sharpimmamir2
 
1669958779195.pdf
1669958779195.pdf1669958779195.pdf
1669958779195.pdfvenud11
 
Java tutorial for Beginners and Entry Level
Java tutorial for Beginners and Entry LevelJava tutorial for Beginners and Entry Level
Java tutorial for Beginners and Entry LevelRamrao Desai
 
Flow of control C ++ By TANUJ
Flow of control C ++ By TANUJFlow of control C ++ By TANUJ
Flow of control C ++ By TANUJTANUJ ⠀
 
Chapter 7 - Defining Your Own Classes - Part II
Chapter 7 - Defining Your Own Classes - Part IIChapter 7 - Defining Your Own Classes - Part II
Chapter 7 - Defining Your Own Classes - Part IIEduardo Bergavera
 
Learning Java 1 – Introduction
Learning Java 1 – IntroductionLearning Java 1 – Introduction
Learning Java 1 – Introductioncaswenson
 
Object oriented programming system with C++
Object oriented programming system with C++Object oriented programming system with C++
Object oriented programming system with C++msharshitha03s
 

Similar to Ppt on java basics (20)

Keyword of java
Keyword of javaKeyword of java
Keyword of java
 
Inheritance
InheritanceInheritance
Inheritance
 
Polymorphism in C# Function overloading in C#
Polymorphism in C# Function overloading in C#Polymorphism in C# Function overloading in C#
Polymorphism in C# Function overloading in C#
 
EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...
EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...
EContent_11_2023_04_09_11_30_38_Unit_3_Objects_and_Classespptx__2023_03_20_12...
 
MODULE_3_Methods and Classes Overloading.pptx
MODULE_3_Methods and Classes Overloading.pptxMODULE_3_Methods and Classes Overloading.pptx
MODULE_3_Methods and Classes Overloading.pptx
 
Programming in Arduino (Part 2)
Programming in Arduino  (Part 2)Programming in Arduino  (Part 2)
Programming in Arduino (Part 2)
 
Java session4
Java session4Java session4
Java session4
 
Repetition Structure
Repetition StructureRepetition Structure
Repetition Structure
 
Introduction to c sharp
Introduction to c sharpIntroduction to c sharp
Introduction to c sharp
 
C Programming Unit-2
C Programming Unit-2C Programming Unit-2
C Programming Unit-2
 
1669958779195.pdf
1669958779195.pdf1669958779195.pdf
1669958779195.pdf
 
Java loops
Java loopsJava loops
Java loops
 
class object.pptx
class object.pptxclass object.pptx
class object.pptx
 
Java tutorial for Beginners and Entry Level
Java tutorial for Beginners and Entry LevelJava tutorial for Beginners and Entry Level
Java tutorial for Beginners and Entry Level
 
Flow of control C ++ By TANUJ
Flow of control C ++ By TANUJFlow of control C ++ By TANUJ
Flow of control C ++ By TANUJ
 
Chapter 7 - Defining Your Own Classes - Part II
Chapter 7 - Defining Your Own Classes - Part IIChapter 7 - Defining Your Own Classes - Part II
Chapter 7 - Defining Your Own Classes - Part II
 
Learning Java 1 – Introduction
Learning Java 1 – IntroductionLearning Java 1 – Introduction
Learning Java 1 – Introduction
 
C++ theory
C++ theoryC++ theory
C++ theory
 
Object oriented programming system with C++
Object oriented programming system with C++Object oriented programming system with C++
Object oriented programming system with C++
 
Ast transformation
Ast transformationAst transformation
Ast transformation
 

Recently uploaded

Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...apidays
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 

Recently uploaded (20)

Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 

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