Sub topic 8
The unnamed pattern: ignoring a whole record component
This chapter assumes you know Java records, instanceof patterns, and the type and record patterns from the previous chapter. It also assumes you know what an unnamed pattern variable (Type _) is. The code needs Java 22 or later.
The problem: components you don't care about
A record pattern pulls a record apart and binds each component to a variable. Often you need only one or two components. Without _, you have to invent names for the rest, and a reader then wonders whether those names are used somewhere.
Here is a record pattern written with named variables for everything:
record Point(int x, int y) {}
record Line(Point start, Point end) {}
class Geometry {
static int startXNamed(Object o) {
if (o instanceof Line(Point(int x, int y), Point end)) {
return x;
}
return -1;
}
}Only x is used. The variables y and end are noise.
The unnamed pattern
The unnamed pattern is a bare _ placed where a nested pattern would go. It matches any value of the component and binds nothing. It has no type and no name.
record Point(int x, int y) {}
record Line(Point start, Point end) {}
class Geometry {
static int startX(Object o) {
if (o instanceof Line(Point(int x, _), _)) {
return x;
}
return -1;
}
public static void main(String[] args) {
System.out.println(startX(new Line(new Point(3, 4), new Point(5, 6))));
System.out.println(startX("not a line"));
}
}This prints 3, then -1. The pattern Line(Point(int x, _), _) does the following:
- It tests that the value is a
Line. - It tests that
startis aPointand binds itsxcomponent tox. - It accepts the
ycomponent and the wholeendcomponent without looking at them.
Unnamed pattern versus unnamed pattern variable
These two forms look similar but do different things.
| Form | Example | Has a type? | Tests the type? |
|---|---|---|---|
| Unnamed pattern variable | `Point _` | Yes | Yes |
| Unnamed pattern | `_` | No | No |
Point _ is a type pattern. It checks that the value is a Point and then discards the match. The bare _ checks nothing and matches every value. Inside Line(Point _, Point _), both forms behave the same, because the component types are already Point. The difference shows when a component has a broader type, such as Object, and you want to require a specific type.
You can also use _ in a switch:
record Point(int x, int y) {}
record Line(Point start, Point end) {}
class Classify {
static String kind(Object o) {
return switch (o) {
case Line(Point(int x, _), _) when x == 0 -> "line starting on the y-axis";
case Line(_, _) -> "some other line";
case Point(_, _) -> "a point";
default -> "something else";
};
}
public static void main(String[] args) {
System.out.println(kind(new Line(new Point(0, 1), new Point(2, 3))));
System.out.println(kind(new Line(new Point(1, 1), new Point(2, 3))));
System.out.println(kind(new Point(1, 1)));
System.out.println(kind(42));
}
}The output is:
line starting on the y-axis
some other line
a point
something elseThings to watch for
The unnamed pattern also matches null components. A record Line(null, null) matches Line(_, _). Only the outer value is null-checked, because instanceof and type tests fail on a null top-level value. Nested components have no such check when the pattern is total for their type.
Another common mistake is to expect _ to bind a value you can read later. It does not. Writing return _; is a compile-time error, because _ is not a name.
Exercise
Given record Pair(Object first, Object second) {}, write a method static boolean firstIsString(Object o) that returns true only when o is a Pair whose first component is a String. Use the unnamed pattern for the second component. Does it matter whether you write _ or Object _ for the second component?
Show answer
static boolean firstIsString(Object o) { return o instanceof Pair(String , ); } It does not matter for the second component. Its declared type is Object, so both _ and Object _ match every value, including null. It would matter if you wrote String _ there, because that would also test the type and reject non-String values. The first component needs String _ (or String s), because the type test is the point of the check.