Record patterns (destructuring)
Destructure records directly in patterns — extract fields in one step.
Code Comparison
✕ Java 8
if (obj instanceof Point) {
Point p = (Point) obj;
int x = p.getX();
int y = p.getY();
System.out.println(x + y);
}
✓ Java 21+
if (obj instanceof Point(int x, int y)) {
System.out.println(x + y);
}
Why the modern way wins
Direct extraction
Access record components without calling accessors manually.
Nestable
Patterns can nest — match inner records in a single expression.
Compact code
Five lines become two — less ceremony, same clarity.
Old Approach
Manual Access
Modern Approach
Destructuring
Since JDK
21
Difficulty
intermediate
JDK Support
Record patterns (destructuring)
Available
Widely available since JDK 21 LTS (Sept 2023)
How it works
Record patterns let you decompose a record's components directly in instanceof and switch. Nested patterns are supported too, enabling deep matching without intermediate variables.