Java – For Loop
For Loop: Iterates for a specific number of times.Example: for (int i = 1; i
For Loop: Iterates for a specific number of times.Example: for (int i = 1; i
Calculate the sum of numbers:Example: int sum = 0;for (int i = 1; i
While Loop: Executes as long as the condition is true.Example: int i = 1;while (i
Check if a number is prime:Example: int num = 29;int i = 2;boolean isPrime = true;while (i
Do-While Loop: Executes at least once before condition check.Example: int i = 1;do { System.out.println(i); i++;} while (i
Validate user input with do-while loop:Example: Scanner sc = new Scanner(System.in);int input;do { System.out.print("Enter a positive number: "); input = sc.nextInt();} while (input
Sealed Classes restrict which classes can extend or implement them.Example: sealed class Shape permits Circle, Rectangle {}final class Circle extends Shape {}final class Rectangle extends Shape {}
Java 19 introduced Virtual Threads, making it easier to handle concurrency.Example: try (var executor = Executors.newVirtualThreadPerTaskExecutor()) { executor.submit(() -> System.out.println("Hello from Virtual Thread"));}
Java 9 introduced the Module System, allowing developers to modularize applications and improve performance.Example: module mymodule { exports com.example.myapp;}
The Optional class helps handle null values and avoid NullPointerException.Example: Optional name = Optional.ofNullable(null);System.out.println(name.orElse("Default Name"));