SlideShare una empresa de Scribd logo
1 de 24
By
javawithease
 Object
 Classes
 Inheritance
 Abstraction
 Encapsulation
 Polymorphism
What Is a Class?
A class is the blueprint from which individual objects
are created. In the real world, you'll often find many
individual objects all of the same kind. Each bicycle
was built from the same set of blueprints and therefore
contains the same components. In object-oriented
terms, we say that your bicycle is an instance of the
class of objects known as bicycles.
What Is Inheritance?
Passing the basic property of a parent class into its child
class is known as Inheritance.
Example:
Mountain bikes, road bikes, and tandem bikes, for
example, all share the characteristics of bicycles
(current speed, current pedal cadence, current gear).
What Is Abstraction?
Abstraction is the force that hide private implementation
details behind public interfaces. Or You can say that
showing only relevant information is known as
Abstraction.
Example:
 Withdrawing money from ATM machine is a good
example of abstraction.
What Is Encapsulation?
Hiding the complexity or wrapping of data into a single
capsule or module is known as Encapsulation.
To achieve encapsulation in your Java program you
should declare all the instance variable as private,
and the method that use these variable as public
What Is Polymorphism?
What Is a Package?
A package is a namespace that organizes a set of
related classes and interfaces. Conceptually you can
think of packages as being similar to different folders
on your computer.
What are the four OOPs principles and define them?
What is inheritance?
What is Abstraction?
What is multiple inheritance?
What can be the state and behavior of a computer?
In Java Programming ,an identifier is a name of a
variable , a class, or a method.
Identifier starts with a letter, underscore, or dollar
sign($), subsequent character can be digits
Followings are valid identifier:
 Identifier
 userName
 User_name
 _sys_var1
 $change
 * not used
 ** added in 1.2
 *** added in 1.4
There are eight primitive types in Java Programming
Operators are special symbols that perform
specific operations on one, two, or three
operands, and then return a result. There are:
 Arithmetic Operators
 Unary Operators
 Assignment Operator
 Bitwise and Bit shift Operator
 Relational and Conditional operator
Expressions
An expression is a construct made up of variables,
operators, and method invocations, which are
constructed according to the syntax of the language,
that evaluates to a single value.
int result = 1 + 2;
Statements
Statements are roughly equivalent to sentences in
natural languages. A statement forms a complete unit
of execution. The following types of expressions can be
made into a statement by terminating the expression
with a semicolon (;).
Blocks
A block is a group of zero or more statements between
balanced braces and can be used anywhere a single
statement is allowed.
class BlockDemo
{
public static void main(String[] args)
{
boolean condition = true;
if (condition) { // begin block 1
System.out.println("Condition is true.");
} // end block one
else { // begin block 2
System.out.println("Condition is false.");
} // end block 2
}
}
 The if-then and if-then-else Statements
 The switch Statement
 The while and do-while Statements
 The for Statement
The if-then statement is the most basic of all the control flow
statements. It tells your program to execute a certain section of code
only if a particular test evaluates to true.
void applyBrakes()
{
if (isMoving)
{ // the "if" clause: bicycle must be moving
currentSpeed--; // the "then" clause: decrease current speed
}
}
The if-then-else statement provides a secondary path of
execution when an "if" clause evaluates to false.
void applyBrakes()
{
if (isMoving)
{
currentSpeed--;
}
else {
System.err.println("The bicycle has already stopped!");
}
}
class IfElseDemo
{
public static void main(String[] args) {
int testscore = 76;
char grade;
if (testscore >= 90) {
grade = 'A';
}
else if (testscore >= 80) {
grade = 'B';
}
else if (testscore >= 70) {
grade = 'C';
}
else if (testscore >= 60) {
grade = 'D';
}
else { grade = 'F'; }
System.out.println("Grade = " + grade);
} }
Unlike if-then and if-then-else statements, the switch
statement can have a number of possible execution
paths. A switch works with the byte, short, char, and int
primitive data types.
public class SwitchDemo {
public static void main(String[] args) {
int day = 4;
String dayString;
switch (month) {
case 1: dayString = "Sunday"; break;
case 2: dayString = "Monday"; break;
case 3: dayString = "Tuesday"; break;
case 4: dayString = "Wednesday"; break;
case 5: dayString = "Thursday"; break;
case 6: dayString = "Friday"; break;
case 7: dayString = "Saturday"; break;
default:dayString = "Invalid day"; break;
}
System.out.println(dayString);
}
}

Más contenido relacionado

La actualidad más candente

Introduction to java beans
Introduction to java beansIntroduction to java beans
Introduction to java beansHitesh Parmar
 
Core java concepts
Core java  conceptsCore java  concepts
Core java conceptsRam132
 
Constructors in java
Constructors in javaConstructors in java
Constructors in javachauhankapil
 
JRE , JDK and platform independent nature of JAVA
JRE , JDK and platform independent nature of JAVAJRE , JDK and platform independent nature of JAVA
JRE , JDK and platform independent nature of JAVAMehak Tawakley
 
Java Presentation
Java PresentationJava Presentation
Java Presentationpm2214
 
Java Programming for Designers
Java Programming for DesignersJava Programming for Designers
Java Programming for DesignersR. Sosa
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVAAbhilash Nair
 
Training on Core java | PPT Presentation | Shravan Sanidhya
Training on Core java | PPT Presentation | Shravan SanidhyaTraining on Core java | PPT Presentation | Shravan Sanidhya
Training on Core java | PPT Presentation | Shravan SanidhyaShravan Sanidhya
 
Object oriented programming
Object oriented programmingObject oriented programming
Object oriented programmingAmit Soni (CTFL)
 
Java interfaces & abstract classes
Java interfaces & abstract classesJava interfaces & abstract classes
Java interfaces & abstract classesShreyans Pathak
 
object oriented Programming ppt
object oriented Programming pptobject oriented Programming ppt
object oriented Programming pptNitesh Dubey
 
Hibernate Presentation
Hibernate  PresentationHibernate  Presentation
Hibernate Presentationguest11106b
 

La actualidad más candente (20)

Java features
Java featuresJava features
Java features
 
Introduction to java beans
Introduction to java beansIntroduction to java beans
Introduction to java beans
 
Core java concepts
Core java  conceptsCore java  concepts
Core java concepts
 
Constructors in java
Constructors in javaConstructors in java
Constructors in java
 
Java Basics
Java BasicsJava Basics
Java Basics
 
JRE , JDK and platform independent nature of JAVA
JRE , JDK and platform independent nature of JAVAJRE , JDK and platform independent nature of JAVA
JRE , JDK and platform independent nature of JAVA
 
Java platform
Java platformJava platform
Java platform
 
Java Presentation
Java PresentationJava Presentation
Java Presentation
 
Java Programming for Designers
Java Programming for DesignersJava Programming for Designers
Java Programming for Designers
 
OOP java
OOP javaOOP java
OOP java
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVA
 
Training on Core java | PPT Presentation | Shravan Sanidhya
Training on Core java | PPT Presentation | Shravan SanidhyaTraining on Core java | PPT Presentation | Shravan Sanidhya
Training on Core java | PPT Presentation | Shravan Sanidhya
 
Object oriented programming
Object oriented programmingObject oriented programming
Object oriented programming
 
Oops in Java
Oops in JavaOops in Java
Oops in Java
 
Java interfaces & abstract classes
Java interfaces & abstract classesJava interfaces & abstract classes
Java interfaces & abstract classes
 
object oriented Programming ppt
object oriented Programming pptobject oriented Programming ppt
object oriented Programming ppt
 
core java
core javacore java
core java
 
Hibernate Presentation
Hibernate  PresentationHibernate  Presentation
Hibernate Presentation
 
Java program structure
Java program structure Java program structure
Java program structure
 
Function in C
Function in CFunction in C
Function in C
 

Destacado

oops concept in java | object oriented programming in java
oops concept in java | object oriented programming in javaoops concept in java | object oriented programming in java
oops concept in java | object oriented programming in javaCPD INDIA
 
Core Java Concepts
Core Java ConceptsCore Java Concepts
Core Java Conceptsmdfkhan625
 
Programming with Java: the Basics
Programming with Java: the BasicsProgramming with Java: the Basics
Programming with Java: the BasicsJussi Pohjolainen
 
Introduction to basics of java
Introduction to basics of javaIntroduction to basics of java
Introduction to basics of javavinay arora
 
Basics of java 2
Basics of java 2Basics of java 2
Basics of java 2Raghu nath
 
Java Course 3: OOP
Java Course 3: OOPJava Course 3: OOP
Java Course 3: OOPAnton Keks
 
Java Course 2: Basics
Java Course 2: BasicsJava Course 2: Basics
Java Course 2: BasicsAnton Keks
 
Basics of file handling
Basics of file handlingBasics of file handling
Basics of file handlingpinkpreet_kaur
 
Core java Basics
Core java BasicsCore java Basics
Core java BasicsRAMU KOLLI
 
Core Java Basics
Core Java BasicsCore Java Basics
Core Java Basicsmhtspvtltd
 
Java basics part 1
Java basics part 1Java basics part 1
Java basics part 1Kevin Rowan
 

Destacado (20)

OOPs in Java
OOPs in JavaOOPs in Java
OOPs in Java
 
Oop java
Oop javaOop java
Oop java
 
oops concept in java | object oriented programming in java
oops concept in java | object oriented programming in javaoops concept in java | object oriented programming in java
oops concept in java | object oriented programming in java
 
Core Java Concepts
Core Java ConceptsCore Java Concepts
Core Java Concepts
 
Programming with Java: the Basics
Programming with Java: the BasicsProgramming with Java: the Basics
Programming with Java: the Basics
 
Introduction to basics of java
Introduction to basics of javaIntroduction to basics of java
Introduction to basics of java
 
Java basics notes
Java basics notesJava basics notes
Java basics notes
 
Basics of java 2
Basics of java 2Basics of java 2
Basics of java 2
 
Java Course 3: OOP
Java Course 3: OOPJava Course 3: OOP
Java Course 3: OOP
 
Java Basics
Java BasicsJava Basics
Java Basics
 
Java Course 2: Basics
Java Course 2: BasicsJava Course 2: Basics
Java Course 2: Basics
 
PALASH SL GUPTA
PALASH SL GUPTAPALASH SL GUPTA
PALASH SL GUPTA
 
Java basics
Java basicsJava basics
Java basics
 
2. Basics of Java
2. Basics of Java2. Basics of Java
2. Basics of Java
 
Basics of file handling
Basics of file handlingBasics of file handling
Basics of file handling
 
Core java Basics
Core java BasicsCore java Basics
Core java Basics
 
Java basics
Java basicsJava basics
Java basics
 
Java Basics
Java BasicsJava Basics
Java Basics
 
Core Java Basics
Core Java BasicsCore Java Basics
Core Java Basics
 
Java basics part 1
Java basics part 1Java basics part 1
Java basics part 1
 

Similar a Java oops and fundamentals (20)

Synapseindia reviews.odp.
Synapseindia reviews.odp.Synapseindia reviews.odp.
Synapseindia reviews.odp.
 
java tr.docx
java tr.docxjava tr.docx
java tr.docx
 
Java tut1
Java tut1Java tut1
Java tut1
 
Java tut1 Coderdojo Cahersiveen
Java tut1 Coderdojo CahersiveenJava tut1 Coderdojo Cahersiveen
Java tut1 Coderdojo Cahersiveen
 
Java tut1
Java tut1Java tut1
Java tut1
 
Javatut1
Javatut1 Javatut1
Javatut1
 
Java Tutorial
Java TutorialJava Tutorial
Java Tutorial
 
Java tutorial PPT
Java tutorial PPTJava tutorial PPT
Java tutorial PPT
 
Java tutorial PPT
Java tutorial  PPTJava tutorial  PPT
Java tutorial PPT
 
Java tut1
Java tut1Java tut1
Java tut1
 
Tutorial java
Tutorial javaTutorial java
Tutorial java
 
Java Tut1
Java Tut1Java Tut1
Java Tut1
 
Java Tutorial
Java TutorialJava Tutorial
Java Tutorial
 
Java Tutorial | My Heart
Java Tutorial | My HeartJava Tutorial | My Heart
Java Tutorial | My Heart
 
Java
JavaJava
Java
 
Android Application Development - Level 3
Android Application Development - Level 3Android Application Development - Level 3
Android Application Development - Level 3
 
Java tutorials
Java tutorialsJava tutorials
Java tutorials
 
Java basics and java variables
Java basics and java variablesJava basics and java variables
Java basics and java variables
 
Javascript sivasoft
Javascript sivasoftJavascript sivasoft
Javascript sivasoft
 
Introduction of Object Oriented Programming Language using Java. .pptx
Introduction of Object Oriented Programming Language using Java. .pptxIntroduction of Object Oriented Programming Language using Java. .pptx
Introduction of Object Oriented Programming Language using Java. .pptx
 

Último

Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
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
 
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
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
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
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 

Último (20)

Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
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
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 

Java oops and fundamentals

  • 2.  Object  Classes  Inheritance  Abstraction  Encapsulation  Polymorphism
  • 3. What Is a Class? A class is the blueprint from which individual objects are created. In the real world, you'll often find many individual objects all of the same kind. Each bicycle was built from the same set of blueprints and therefore contains the same components. In object-oriented terms, we say that your bicycle is an instance of the class of objects known as bicycles.
  • 4. What Is Inheritance? Passing the basic property of a parent class into its child class is known as Inheritance. Example: Mountain bikes, road bikes, and tandem bikes, for example, all share the characteristics of bicycles (current speed, current pedal cadence, current gear).
  • 5. What Is Abstraction? Abstraction is the force that hide private implementation details behind public interfaces. Or You can say that showing only relevant information is known as Abstraction. Example:  Withdrawing money from ATM machine is a good example of abstraction.
  • 6. What Is Encapsulation? Hiding the complexity or wrapping of data into a single capsule or module is known as Encapsulation. To achieve encapsulation in your Java program you should declare all the instance variable as private, and the method that use these variable as public
  • 8. What Is a Package? A package is a namespace that organizes a set of related classes and interfaces. Conceptually you can think of packages as being similar to different folders on your computer.
  • 9.
  • 10. What are the four OOPs principles and define them? What is inheritance? What is Abstraction? What is multiple inheritance? What can be the state and behavior of a computer?
  • 11.
  • 12. In Java Programming ,an identifier is a name of a variable , a class, or a method. Identifier starts with a letter, underscore, or dollar sign($), subsequent character can be digits Followings are valid identifier:  Identifier  userName  User_name  _sys_var1  $change
  • 13.  * not used  ** added in 1.2  *** added in 1.4
  • 14. There are eight primitive types in Java Programming
  • 15. Operators are special symbols that perform specific operations on one, two, or three operands, and then return a result. There are:  Arithmetic Operators  Unary Operators  Assignment Operator  Bitwise and Bit shift Operator  Relational and Conditional operator
  • 16.
  • 17. Expressions An expression is a construct made up of variables, operators, and method invocations, which are constructed according to the syntax of the language, that evaluates to a single value. int result = 1 + 2; Statements Statements are roughly equivalent to sentences in natural languages. A statement forms a complete unit of execution. The following types of expressions can be made into a statement by terminating the expression with a semicolon (;).
  • 18. Blocks A block is a group of zero or more statements between balanced braces and can be used anywhere a single statement is allowed. class BlockDemo { public static void main(String[] args) { boolean condition = true; if (condition) { // begin block 1 System.out.println("Condition is true."); } // end block one else { // begin block 2 System.out.println("Condition is false."); } // end block 2 } }
  • 19.  The if-then and if-then-else Statements  The switch Statement  The while and do-while Statements  The for Statement
  • 20. The if-then statement is the most basic of all the control flow statements. It tells your program to execute a certain section of code only if a particular test evaluates to true. void applyBrakes() { if (isMoving) { // the "if" clause: bicycle must be moving currentSpeed--; // the "then" clause: decrease current speed } }
  • 21. The if-then-else statement provides a secondary path of execution when an "if" clause evaluates to false. void applyBrakes() { if (isMoving) { currentSpeed--; } else { System.err.println("The bicycle has already stopped!"); } }
  • 22. class IfElseDemo { public static void main(String[] args) { int testscore = 76; char grade; if (testscore >= 90) { grade = 'A'; } else if (testscore >= 80) { grade = 'B'; } else if (testscore >= 70) { grade = 'C'; } else if (testscore >= 60) { grade = 'D'; } else { grade = 'F'; } System.out.println("Grade = " + grade); } }
  • 23. Unlike if-then and if-then-else statements, the switch statement can have a number of possible execution paths. A switch works with the byte, short, char, and int primitive data types.
  • 24. public class SwitchDemo { public static void main(String[] args) { int day = 4; String dayString; switch (month) { case 1: dayString = "Sunday"; break; case 2: dayString = "Monday"; break; case 3: dayString = "Tuesday"; break; case 4: dayString = "Wednesday"; break; case 5: dayString = "Thursday"; break; case 6: dayString = "Friday"; break; case 7: dayString = "Saturday"; break; default:dayString = "Invalid day"; break; } System.out.println(dayString); } }