Senior
What new features of Java 17/21 do you use?
sobes.tech AI
Answer from AI
Java 17 and 21 introduced several interesting features that I use in my projects:
-
Pattern Matching for instanceof (Java 17): simplifies type checking and casting, making the code cleaner.
-
Sealed Classes (Java 17): allow restricting inheritance, which improves control over class hierarchies.
-
Records (Java 17): a convenient way to create immutable classes with minimal boilerplate code.
-
Foreign Function & Memory API (experimental in Java 21): simplifies interaction with native code without JNI.
-
Virtual Threads (Java 21, Project Loom): significantly simplify multithreading and increase application scalability.
Example of using Records:
public record User(String name, int age) {}
User user = new User("Alice", 30);
System.out.println(user.name());
These features help write more concise, safe, and performant code.