Language Średniozaawansowany

Destructure records directly in patterns — extract fields in one step.

✕ 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ć.
🎯

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
Record patterns (destructuring)
Dostępne

Widely available since JDK 21 LTS (Sept 2023)

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.

Udostępnij 𝕏 🦋 in