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!");}
The Java Collections Framework provides reusable data structures like ArrayList, HashMap, and HashSet.Example: ArrayList list = new ArrayList();list.add("Apple");list.add("Banana");System.out.println(list);
Generics enable type-safe programming by specifying data types for classes and methods.Example: ArrayList numbers = new ArrayList();numbers.add(10);System.out.println(numbers);
The Streams API introduced in Java 8 enables functional-style operations on collections.Example: List names = Arrays.asList("Alice", "Bob", "Charlie");names.stream().filter(name -> name.startsWith("A")) .forEach(System.out::println);