An underscore stands in for a variable or pattern component you must declare but will not use.
The problem it solved
Code was littered with names like ignored or unused, which read as meaningful and invited static-analysis warnings for being unread.
How you did it before
Naming the variable something like ignored, or destructuring every record component even when only one mattered.
// Requires JDK 22 — not compiled on this build.
record Point(int x, int y) {}
Object o = new Point(3, 4);
if (o instanceof Point(int x, _)) {
System.out.println("x only: " + x);
}
Asked as
- Where can you use an unnamed variable?
- Why is _ better than naming it ignored?