Junior — Middle
Describe the new features introduced in Java versions 15 and 17
sobes.tech AI
Answer from AI
In Java 15 and Java 17, several important features and improvements were introduced:
Java 15:
- Records (preview) — a compact way to define immutable classes with automatic generation of
equals(),hashCode(), andtoString()methods. - Sealed Classes (preview) — allow restricting the inheritance hierarchy by specifying which classes can extend a given class.
- Text Blocks — multi-line string literals that simplify working with multi-line text.
- Improvements in Garbage Collector (e.g., ZGC became more performant).
Java 17 (LTS):
- Sealed Classes (stable) — final introduction of sealed classes.
- Pattern Matching for instanceof — simplifies type checking and casting.
- Strongly Encapsulate JDK Internals — enhanced encapsulation of internal APIs.
- Performance and security improvements.
Example of using Records (Java 16+):
public record Point(int x, int y) {}
Point p = new Point(1, 2);
System.out.println(p.x()); // 1
This allows quickly creating classes with immutable fields without extra boilerplate code.