Record patterns (destructuring)
Home / Language / Record patterns (destructuring)
Code Comparison
if (obj instanceof Point) {
Point p = (Point) obj;
int x = p.getX();
int y = p.getY();
System.out.println(x + y);
}
if (obj instanceof Point(int x, int y)) {
IO.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
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.
Related Documentation
Proof