Sub topic 12
Ignoring Components with Unnamed Patterns (Java 22)
Assume you know Java classes, records, inheritance, switch expressions, and basic generics. You also need JDK 22 or newer. Record patterns were finalized in Java 21. Unnamed patterns were finalized in Java 22.
Why unnamed patterns matter
A record pattern can extract the components of a record. Sometimes you need to match a component but do not need its value. Before Java 22, you had to give that component a variable name, even if you never used it.
An unnamed pattern is an underscore character. It matches a value and deliberately discards it. The underscore does not create a variable, so you cannot refer to that value later.
record Point(int x, int y) {}
static int xCoordinate(Object value) {
return switch (value) {
case Point(int x, _) -> x;
default -> 0;
};
}The pattern matches a Point, saves its x component in x, and ignores its y component. The pattern is clearer than naming the unused component y.
Unnamed patterns in nested records
Unnamed patterns are especially useful when records contain other records. Consider a box that stores a Shape:
record Box<T>(T content) {}
interface Shape {}
record Circle(double radius) implements Shape {}
record Rectangle(double width, double height) implements Shape {}
static String describe(Box<Shape> box) {
return switch (box) {
case Box<Shape>(Circle _) -> "box holding a circle";
case Box<Shape>(var _) -> "box holding another shape";
};
}The first case checks whether the content is a Circle and ignores the Circle's radius. The second case matches any other Shape and ignores its value.
The difference between the two component patterns is useful:
| Pattern | Meaning |
|---|---|
| Circle _ | Match a Circle and discard it |
| var shape | Match any value and store it in shape |
| var _ | Match any value and discard it |
The unnamed pattern still performs type checking. Circle _ accepts only Circle values. It does not match a Rectangle.
How matching flows
The outer pattern first checks that the value is a Box. The nested pattern then checks the content.
Unnamed patterns also work in ordinary type-pattern switch cases:
static String shapeName(Shape shape) {
return switch (shape) {
case Circle _ -> "circle";
case Rectangle _ -> "rectangle";
};
}This method cares only about the runtime type. It does not need the radius, width, or height. The switch remains exhaustive because the Shape hierarchy has only these two implementations.
Choosing between var and underscore
Use an underscore when the value has no role in the result and should not be retained. Use var when the value will be passed to another method, compared, or returned.
Unnamed patterns can also communicate intent. A named but unused variable may make readers wonder whether the code forgot to use it. An underscore states that ignoring the value is deliberate.
Exercise
What does Point(_, int y) mean in a record pattern?
Show answer
It matches a Point, discards its x component, and binds its y component to the variable y.