The latest non-LTS version of Java is Java 23, which was released in September
2024. The latest long-term support (LTS) version is Java 21, released in September
2023. Java 24 is expected to be released in March 2025.
1. Decision-Making Statements
Used to execute code based on conditions.
Statement Description Example
if Executes a block if if (a > b) {...}
condition is true
if-else Executes one block if true, if (a > b) {...} else
another if false {...}
nested if if inside another if if (a > b) { if (a > c)
{...} }
if-else-if Multiple conditions if (a > b) {...} else if
(...) {...}
switch Selects a block to execute switch (day) { case 1: ...
based on a value }
2. Looping Statements
Used to repeat a block of code.
Statement Description Example
for Loops a fixed number of for (int i = 0; i < 5; i++)
times {...}
while Loops while condition is while (i < 5) {...}
true
do-while Loops at least once do {...} while (i < 5);
Enhanced for Loops through for (int num : arr) {...}
(for-each) arrays/collections
3. Jump Statements
Used to transfer control to another part of the code.
Statement Description Example
break Exits from loop or switch break;
block
continue Skips the current iteration continue;
return Exits from a method return;
Advanced Decision Making in Java
After covering 'if', 'if-else', 'nested if', and 'switch-case', here are the next decision-making
constructs in Java with examples:
1. Ternary Operator ( ?: ) A shorthand for if-else.
Syntax:
condition ? expression1 : expression2;
Example:
public class TernaryExample {
public static void main(String[] args) {
int age = 18;
String result = (age >= 18) ? "Eligible to vote" : "Not eligible";
System.out.println(result);
}
}
Output:
Eligible to vote
2. if inside Loops (for, while)
Using conditions inside loops for decision-making.
Example:
public class EvenOdd {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
if (i % 2 == 0) {
System.out.println(i + " is Even");
} else {
System.out.println(i + " is Odd");
}
}
}
}
Output:
1 is Odd
2 is Even
3 is Odd
4 is Even
5 is Odd
3. Logical Operators with Conditions
Using &&, ||, ! with conditions.
Example:
public class LogicalOperators {
public static void main(String[] args) {
int age = 25;
boolean hasLicense = true;
if (age >= 18 && hasLicense) {
System.out.println("You can drive");
} else {
System.out.println("You cannot drive");
}
}
}
Output:
You can drive
4. break and continue in Decision Making
Example with break:
public class BreakExample {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
if (i == 3) {
break;
}
System.out.println("i = " + i);
}
}
}
Output:
i = 1
i=2
Example with continue:
public class ContinueExample {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
if (i == 3) {
continue;
}
System.out.println("i = " + i);
}
}
}
Output:
i = 1
i = 2
i = 4
i=5
5. Switch Expression (Java 14+)
Switch can also be used as an expression (Java 14+).
Example:
public class SwitchExpression {
public static void main(String[] args) {
int day = 3;
String dayType = switch (day) {
case 1, 7 -> "Weekend";
case 2, 3, 4, 5, 6 -> "Weekday";
default -> "Invalid";
};
System.out.println(dayType);
}
}
Output:
Weekday
If else if
public class GradeChecker {
public static void main(String[] args) {
int marks = 75;
if (marks >= 90) {
System.out.println("Grade: A");
} else if (marks >= 80) {
System.out.println("Grade: B");
} else if (marks >= 70) {
System.out.println("Grade: C");
} else if (marks >= 60) {
System.out.println("Grade: D");
} else {
System.out.println("Grade: F (Fail)");
4 OOPs concepts in Java
1. Encapsulation
Definition: Wrapping data (variables) and methods (functions) into a single unit (class). It
protects data using private access and provides access via public methods.
✅ Example:
class Student {
private String name; // private = data hiding
public void setName(String n) {
name = n;
public String getName() {
return name;
}
public class Main {
public static void main(String[] args) {
Student s = new Student();
s.setName("Amit");
System.out.println("Student Name: " + s.getName());
🔸 Output:
Student Name: Amit
🔷 2. Abstraction
Definition: Hiding internal implementation details and showing only essential features
using abstract classes or interfaces.
✅ Example using abstract class:
java
CopyEdit
abstract class Animal {
abstract void sound(); // abstract method
class Dog extends Animal {
void sound() {
System.out.println("Dog barks");
public class Main {
public static void main(String[] args) {
Animal a = new Dog(); // only essential behavior exposed
a.sound();
🔸 Output:
Dog barks
🔷 3. Inheritance
Definition: One class (child) inherits properties and methods of another class (parent)
using the extends keyword.
✅ Example:
class Vehicle {
void start() {
System.out.println("Vehicle starts");
class Car extends Vehicle {
void speed() {
System.out.println("Car is running fast");
public class Main {
public static void main(String[] args) {
Car c = new Car();
c.start(); // inherited method
c.speed(); // own method
🔸 Output:
Vehicle starts
Car is running fast
🔷 4. Polymorphism
Definition: Ability of an object to take many forms — method overloading and method
overriding.
✴️ a) Method Overloading (Compile-time Polymorphism)
class Math {
int add(int a, int b) {
return a + b;
}
int add(int a, int b, int c) {
return a + b + c;
public class Main {
public static void main(String[] args) {
Math m = new Math();
System.out.println(m.add(10, 20));
System.out.println(m.add(5, 10, 15));
🔸 Output:
30
30
✴️ b) Method Overriding (Runtime Polymorphism)
class Animal {
void sound() {
System.out.println("Animal makes sound");
class Cat extends Animal {
void sound() {
System.out.println("Cat meows");
}
public class Main {
public static void main(String[] args) {
Animal a = new Cat(); // Parent reference, child object
a.sound();
🔸 Output:
Cat meows
WORKOUT
Write a Java program to check whether a number is positive or negative using if statement.
Write a Java program to check whether a person is eligible to vote (age ≥ 18) using if-else.
Write a Java program to find the largest of two numbers using if-else.
Write a Java program to determine whether a number is even or odd using if-else.
Write a Java program to assign grades based on marks using if-else-if:
Marks ≥ 90: A
Marks ≥ 80: B
Marks ≥ 70: C
Marks ≥ 60: D
Below 60: F
Write a Java program to check if a number is divisible by both 5 and 11 using if-else-if.
Write a Java program using nested if to check:If a number is positive, check if it's even or
odd.
Write a Java program using nested if to check login credentials:If username is correct, then
check if password is correct.
9. Write a Java program using switch to print the name of the day based on
number (1–7):
○ 1 = Sunday, 2 = Monday, ..., 7 = Saturday
10.Write a Java program using switch to perform a basic calculator:
● Take two numbers and an operator (+, -, *, /), and perform the operation.
11.Write a Java program using switch to display the number of days in a month
(use case for months).
12.Write a Java program using switch to display season for a given month
number:
● 12, 1, 2 = Winter
● 3, 4, 5 = Spring
● 6, 7, 8 = Summer
● 9, 10, 11 = Autumn
Create an object
// Define a Car class
class Car {
// Method to display a message
void startEngine() {
System.out.println("The car engine is starting...");
// Main class
public class CarExample {
public static void main(String[] args) {
// Create an object of Car class
Car myCar = new Car();
// Call method using object
myCar.startEngine();
// Define a class
class Student {
// Attributes
String name;
int age;
// Method to display student details
void displayInfo() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
// Main class
public class ObjectExample {
public static void main(String[] args) {
// Create an object of Student class
Student s1 = new Student();
// Set values to object variables
s1.name = "Anu";
s1.age = 21;
// Call method using object
s1.displayInfo();