What changed in modern Java (8 to 21) that every developer should know?
Answer
- Java 8: lambdas, streams,
Optional, default methods, java.time API. - Java 9 to 11: modules,
var for local variables, List.of, HTTP client; Java 11 is an LTS (long-term support). - Java 14 to 17: records, switch expressions, text blocks, sealed classes, pattern matching for
instanceof; Java 17 is an LTS. - Java 21 (LTS): virtual threads, record patterns, pattern matching for
switch, sequenced collections. - Say: "I mainly target the current LTS and use records, streams and
Optional daily."
// Java 17+ examples
record User(String name, int age) {}
String text = """
multi-line
text block""";
Object o = 42;
if (o instanceof Integer i && i > 40) { System.out.println(i); }
String label = switch (day) {
case SATURDAY, SUNDAY -> "weekend";
default -> "weekday";
};
Practice this question in Repaso