Record patterns (destructuring)
Destructure records directly in patterns — extract fields in one step.
Porównanie kodu
✕ 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)) {
IO.println(x + y);
}
Widzisz problem z tym kodem? Daj nam znać.
Dlaczego nowoczesne podejście wygrywa
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.
Stare podejście
Manual Access
Nowoczesne podejście
Destructuring
Od JDK
21
Poziom trudności
Średniozaawansowany
Wsparcie JDK
Record patterns (destructuring)
Dostępne
Widely available since JDK 21 LTS (Sept 2023)
Jak to działa
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.
Powiązana dokumentacja