Java 21: Record patterns deconstruct records

Record patterns bind a record's components directly in instanceof or switch.

Code
record Point(int x, int y) {}
Object o = new Point(3, 4);
if (o instanceof Point(int x, int y)) {
    System.out.println("x+y = " + (x + y));
}
Output
x+y = 7
Advertisement

Run this yourself in the Online Java Compiler, spin up a live REST API in the API Sandbox, or practise with Java interview questions.

Published 2026-07-26