Sub topic 7
Unnamed pattern variables: Type _
This chapter assumes you know Java records, instanceof, and switch. It also assumes you have seen type patterns and record patterns. You need Java 22 or later. In Java 21 the feature is a preview and needs --enable-preview.
The problem: a binding you never read
A type pattern such as Circle c does two jobs. It tests the type, and it declares a variable c that holds the value as that type. Often you need only the test.
return switch (shape) {
case Circle c -> "round";
case Square s -> "has corners";
};Neither c nor s is read. A reader still has to check the right-hand side to be sure of that. Some linters also warn about unused variables.
An unnamed pattern variable removes the binding. You write _ where the name would go.
case Circle _ -> "round";The pattern Circle _ still tests whether the value is a Circle. It declares no variable.
Using it in switch
Here is a complete program you can run with Java 22 or later.
sealed interface Shape permits Circle, Square {}
record Circle(double radius) implements Shape {}
record Square(double side) implements Shape {}
public class Kinds {
static String kind(Shape s) {
return switch (s) {
case Circle _ -> "round";
case Square _ -> "has corners";
};
}
public static void main(String[] args) {
System.out.println(kind(new Circle(1.5)));
System.out.println(kind(new Square(2)));
}
}The output is round and then has corners. Because Shape is sealed, the switch is exhaustive without a default branch. Unnamed pattern variables do not change that check. The compiler still verifies that every permitted subtype is covered.
Using it with instanceof
instanceof accepts the same form.
if (obj instanceof String _) {
System.out.println("got some text");
}This behaves the same as obj instanceof String, which has always been legal. Use the plain form when the pattern is the whole test. Type _ earns its place inside a larger pattern, as the next section shows.
Inside record patterns
A record pattern lists one pattern per component. A component pattern can be a type pattern, so you can make it unnamed.
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, int _), Point _)) {
return x;
}
return -1;
}
}Here x is bound and used. int _ ignores the y of the start point. Point _ checks that the end component is a Point and ignores its value.
You can also use var _. It lets the compiler infer the component type, which is useful when the type name is long.
if (o instanceof Line(Point(var x, var _), var _)) {
return x;
}A pattern like Point _ or var _ still states a type. Every component still has a known type, and the compiler checks it against the record declaration. The next chapter covers the bare _ that skips the type as well.
Common errors
Other mistakes to watch for:
- Using
_in a Java 8 source level or earlier. It was a legal identifier there. From Java 9 it is a compile error, and from Java 22 it is valid only as an unnamed variable or pattern. - Expecting
_to be a shared variable. Each_declares nothing, so you can write several in one pattern without a name clash. InLine(Point _, Point _)there is no duplicate-name error. - Writing
case Circle _ -> ...and expecting the compiler to skip the type check. It does not skip it. Only the variable is dropped.
Exercise
The method below declares two pattern variables that the code never reads. Rewrite its switch so that neither variable is named, and say what stays the same. static String label(Shape s) { return switch (s) { case Circle c -> "curved"; case Square q -> "straight"; }; }
Show answer
Write case Circle _ -> "curved"; and case Square _ -> "straight";. The type tests are unchanged, so the switch still selects by the runtime type of s. It is still exhaustive because Shape is sealed, and the results are the same. Only the unused bindings c and q are gone.