Sub topic 9
Deconstructing Records with Record Patterns
Prerequisites
This chapter assumes you know Java classes, records, interfaces, inheritance, instanceof, switch expressions, and basic type inference. The examples use JDK 21 or newer.
A record is a compact class for carrying data. For example:
record Point(int x, int y) {}
record Line(Point start, Point end) {}Point has two components, x and y. Java generates a constructor, accessors named x() and y(), and other methods such as equals. Line carries two Point values.
Without record patterns, you must test the type, cast the value, call accessors, and repeat the process for nested records. Record patterns combine these steps.
The Basic Record Pattern
A record pattern matches a record and extracts its components:
Point(int x, int y)This pattern means:
- The value must be a
Point. - Its
xcomponent is assigned tox. - Its
ycomponent is assigned toy.
The component variables are available only inside the case where the pattern matched.
Here is a complete example:
public class RecordPatternDemo {
record Point(int x, int y) {}
static String describe(Object value) {
return switch (value) {
case Point(int x, int y) ->
"point at (" + x + ", " + y + ")";
default ->
"not a point";
};
}
public static void main(String[] args) {
System.out.println(describe(new Point(3, 5)));
System.out.println(describe("text"));
}
}The first call produces point at (3, 5). The pattern both checks that the value is a Point and calls its accessors. You do not write a cast or call x() and y() yourself.
A record pattern can also appear in an instanceof condition:
static boolean isOrigin(Object value) {
return value instanceof Point(int x, int y)
&& x == 0
&& y == 0;
}This is useful when you need a boolean condition rather than a switch expression.
Nested Record Patterns
Record components can themselves be records. You can nest patterns to extract data at several levels:
public class LineDemo {
record Point(int x, int y) {}
record Line(Point start, Point end) {}
static boolean isHorizontal(Object value) {
return switch (value) {
case Line(Point(var x1, var y1),
Point(var x2, var y2)) -> y1 == y2;
default -> false;
};
}
public static void main(String[] args) {
System.out.println(
isHorizontal(new Line(new Point(1, 4), new Point(8, 4))));
System.out.println(
isHorizontal(new Line(new Point(1, 4), new Point(8, 7))));
}
}The outer pattern matches Line. Each inner pattern matches a Point. The var patterns let the compiler infer that each extracted value is an int.
A nested pattern is clearer when the operation depends on the data’s structure. It also avoids temporary variables such as line.start().x() and line.end().y().
Choosing Patterns Carefully
Use a record pattern when you need one or more components immediately. If you only need the whole record, a type pattern is simpler:
case Line line -> line.start()If you need only one component, use an unnamed pattern where your Java version supports it, or bind the unused component to a clearly named variable. Keep patterns focused on the data the case actually uses.
Exercise
What does case Point(int x, int y) do in a switch over Object?
Show answer
It checks that the value is a Point, extracts its two components, and binds them to the variables x and y.