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"));
Switch expressions introduced in Java 12 allow concise case statements.Example: int day = 2;String dayName = switch (day) { case 1 -> "Monday"; case 2 -> "Tuesday"; default -> "Invalid Day";};System.out.println(dayName);
Records provide a compact syntax for creating data classes.Example: record Point(int x, int y) {}Point p = new Point(10, 20);System.out.println(p.x() + ", " + p.y());
Java introduced Pattern Matching for type checks with instanceof in Java 16.Example: Object obj = "Hello";if (obj instanceof String s) { System.out.println(s.toUpperCase());}
Loops in Java are used for repeated execution of a block of code. Common types include for, while, and do-while loops.Example: for (int i = 0; i < 5; i++) { System.out.println(i);}
Functions are reusable blocks of code that perform specific tasks. They are defined using the public static void syntax.Example: public static void greet() { System.out.println("Hello, World!");}public static void main(String[] args) { greet();}
Java is an object-oriented programming language that includes Inheritance, Polymorphism, Encapsulation, and Abstraction.Example: class Animal { void sound() { System.out.println("Animal makes a sound"); }}class Dog extends Animal { void sound() { System.out.println("Dog barks"); }}
Exception Handling in Java is used to manage runtime errors using try, catch, finally, and throw statements.Example: try { int result = 10 / 0;} catch (ArithmeticException e) { System.out.println("Division by zero error!");}