Sub topic 13
Next step: record patterns
This chapter assumes you know Java records, instanceof, reference casts, and boolean conditions such as &&. A record pattern extends a type pattern. It checks an object's type and extracts the record's components in the same expression.
From a type pattern to a record pattern
Suppose Point is a record with two components:
record Point(int x, int y) {}A type pattern gives you a Point variable, but you still need to call its accessor methods:
if (value instanceof Point point) {
System.out.println(point.x() + point.y());
}A record pattern goes one step further:
if (value instanceof Point(int x, int y)) {
System.out.println(x + y);
}Point(int x, int y) is a record pattern. It does three things:
- Tests whether
valueis aPoint. - Reads the record components by calling
x()andy(). - Declares
xandywith their component types.
The variables x and y are in scope only where the pattern has definitely matched.
public class RecordPatternDemo {
record Point(int x, int y) {}
record Box(Point point) {}
static String describe(Object value) {
if (value instanceof Point(int x, int y)) {
return "Point at (" + x + ", " + y + ")";
}
return "Not a point";
}
static String describeBox(Object value) {
if (value instanceof Box(Point(int x, int y))) {
return "Boxed point at (" + x + ", " + y + ")";
}
return "Not a box containing a point";
}
public static void main(String[] args) {
System.out.println(describe(new Point(3, 4)));
System.out.println(describe("text"));
System.out.println(describeBox(new Box(new Point(8, 2))));
System.out.println(describeBox(new Box(null)));
}
}The nested pattern Box(Point(int x, int y)) first checks for a Box, then checks its point component, and finally extracts the point's coordinates. If the component is null, the nested Point pattern does not match.
Flow scope still applies
Record-pattern variables follow the same flow-scoping rules as type-pattern variables. They can be used on the right side of && because that side runs only after the pattern succeeds:
static boolean isPositivePoint(Object value) {
return value instanceof Point(int x, int y)
&& x > 0
&& y > 0;
}They cannot be used on the right side of ||. The right side might run when the pattern failed, so the variables would not be available there.
A record pattern can also appear in a negated early-exit guard:
static int coordinateSum(Object value) {
if (!(value instanceof Point(int x, int y))) {
return -1;
}
return x + y;
}After the if statement, both x and y are available because every path that reaches the final statement has matched the pattern.
Relation to pattern matching for switch
Record patterns can be used in switch cases as well as with instanceof. This lets a program select behavior based on a record's type and components:
static String label(Object value) {
return switch (value) {
case Point(int x, int y) -> "Point: " + x + ", " + y;
default -> "Other value";
};
}The same extraction idea is used in both forms. instanceof is useful for a local conditional. Pattern matching for switch is useful when several possible types or shapes need different branches.
Exercise
Write a method that returns "origin" for a Point(0, 0) and "other point" for any other Point, while returning "not a point" for other objects.
Show answer
static String classify(Object value) { if (value instanceof Point(int x, int y)) { return x == 0 && y == 0 ? "origin" : "other point"; } return "not a point"; }