Scrivio Interview library
CodingFoundations6 min read · updated 12 November 2026

Use unnamed variables and patterns (Java 22) to simplify each case: an unused catch parameter, an unused lambda parameter, and a record pattern where only one component matters.

Since Java 22 we can write an underscore wherever a variable must be declared but will never be used: catch (NumberFormatException _) { return defaultValue; }, map.forEach((_, v) -> total.add(v)), and o instanceof Point(var x, _) when only x matters. The underscore declares a variable that cannot be read, several can appear in the same scope, and the feature is final in Java 22 through JEP 456.

Why this question is asked

Java often forces us to name things we do not care about: the exception we only catch to fall back, the key we ignore in a map loop, the component of a record we do not need. Those names add noise, and worse, they make a reader wonder whether the variable is used somewhere. Unnamed variables and patterns fix this with one character. Interviewers ask this coding question to see whether we know the newer syntax and its rules, and whether we can explain the history of the underscore. In this answer, we will recap the problem, rewrite each of the three cases, explain the rules that apply, and look at a few other places the underscore helps.

1A Quick Recap of the Problem

A variable is unused when we declare it only because the language requires a name there. Tools warn about unused locals, but they cannot tell which unused names are deliberate. So code reviewers see a name like e or key, check whether it is used, find that it is not, and move on, every time.

An unnamed variable, written as a single underscore _, says to both the compiler and the reader that the value is intentionally ignored. An unnamed pattern does the same inside a pattern: it matches anything in that position without binding a name. The feature was previewed in Java 21 and became final in Java 22 through JEP 456.

2Case 1: An Unused Catch Parameter

Parsing a number with a fallback is a typical example. We need to catch the exception but do nothing with it.

Java
// before
static int parseOr(String s, int defaultValue) {
try {
return Integer.parseInt(s);
} catch (NumberFormatException e) { // e is never used
return defaultValue;
}
}
// Java 22 and later
static int parseOr(String s, int defaultValue) {
try {
return Integer.parseInt(s);
} catch (NumberFormatException _) {
return defaultValue;
}
}
parseOr("42", 0); // 42
parseOr("abc", 0); // 0

Notice that the type stays; only the name goes. The catch still needs to know which exception it handles. The underscore tells the reader immediately that this is a deliberate fallback, not a forgotten log statement.

3Case 2: An Unused Lambda Parameter

Map.forEach passes both the key and the value to our lambda. When we only need the values, the key parameter is noise.

Java
Map<String, Long> sales = Map.of("north", 120L, "south", 80L);
LongAdder total = new LongAdder();
// before
sales.forEach((region, v) -> total.add(v)); // region is never used
// Java 22 and later
sales.forEach((_, v) -> total.add(v));
System.out.println(total.sum()); // 200

A LongAdder is used here because a lambda can only capture effectively final local variables, so we cannot add to a plain long inside it. The underscore also works when several parameters are unused, such as a BiFunction where we ignore both: (_, _) -> 0. That is new: before, two lambda parameters could not share a name.

4Case 3: A Record Pattern Where One Component Matters

In a record pattern we normally write a pattern for every component. With unnamed patterns, we write _ for the ones we do not need.

Java
record Point(int x, int y) {}
Object o = new Point(3, 7);
// before
if (o instanceof Point(var x, var y)) { // y is never used
System.out.println(x);
}
// Java 22 and later
if (o instanceof Point(var x, _)) {
System.out.println(x); // 3
}
The three cases side by side
PlaceBeforeJava 22 and later
catch parametercatch (NumberFormatException e)catch (NumberFormatException _)
lambda parameter(region, v) -> total.add(v)(_, v) -> total.add(v)
record componentPoint(var x, var y)Point(var x, _)
for loop variablefor (Order o : orders) count++for (Order _ : orders) count++
In every case the type or position stays, and only the unused name disappears.

Notice that _ here is a pattern, not a variable: it matches any value of the component, including null for reference types, and binds nothing. We could also write var _ or int _, which are unnamed pattern variables with a type; plain _ is the shortest and most common form. It is especially helpful with nested patterns, where unused names multiply quickly.

5The Rules and the History of the Underscore

An unnamed variable can never be read or written after its declaration; trying to use _ as an expression is a compile error. Because it introduces no name, several unnamed variables can appear in the same scope, which is why (_, _) -> 0 is legal. The underscore can be used in local variable declarations, for loop variables, try-with-resources resources, catch parameters, lambda parameters and patterns. It cannot be used for fields or method parameters, because those are part of a class's API and are visible elsewhere.

The history explains why this took so long. Up to Java 8, _ was an ordinary identifier, and Java 8 began warning about it. In Java 9 it became a reserved keyword, so code using it as a name stopped compiling. That cleared the way to give it a new meaning, and JEP 456 in Java 22 finally did. Code written before Java 9 that named a variable _ has to be renamed before it can compile on a modern JDK.

Java
try (var _ = lock.acquire()) { // resource needed only for its close()
doWork();
}
int _ = queue.remove(); // side effect wanted, value not
// System.out.println(_); // error: _ cannot be used as an expression

6Follow-up questions

Can we use _ for a method parameter we do not need?

No. Method and constructor parameters are part of the signature that callers and overriding methods see, so they must have names. Unnamed variables are limited to local declarations, catch and lambda parameters, loop variables, resources and patterns.

Does catch (Exception _) mean we swallow the error?

It means we chose not to use the exception object. Whether that is right depends on the case. For a parse fallback it is fine. For anything unexpected, we usually want to log the exception, and then it needs a name.

How is _ different from var _ in a pattern?

Both match any value and bind nothing. var _ and Type _ are unnamed pattern variables, which can state a type, while plain _ is the unnamed pattern, allowed only nested inside a record pattern. At the top level of a case, we must write a type, as in case Circle _.

7Conclusion

In this answer, we used the Java 22 underscore to remove three unused names: catch (NumberFormatException _), map.forEach((_, v) -> total.add(v)), and o instanceof Point(var x, _). The underscore declares a variable or pattern that cannot be read, several can share a scope, and it works in locals, loops, resources, catch and lambda parameters and patterns. It became possible because _ was reserved as a keyword in Java 9, and the feature is final in Java 22 through JEP 456.

What to say in the interviewAbout 80 seconds aloud

Open with

On Java 22 and later I would use the underscore for each of these. For the catch, I write catch (NumberFormatException _) { return defaultValue; }, keeping the type but dropping the name, which tells the reader the fallback is deliberate.

Then, if they want more

For the lambda, map.forEach((_, v) -> total.add(v)), where total is something like a LongAdder since lambdas can only capture effectively final locals. For the record pattern, o instanceof Point(var x, _), where the underscore is an unnamed pattern that matches any value of y and binds nothing.

The rules are that an unnamed variable can never be read, and because it introduces no name, several can appear in the same scope, so (_, _) -> 0 is legal, which it was not before.

It works for locals, loop variables, try-with-resources, catch and lambda parameters and patterns, but not for fields or method parameters, since those are part of the API. The history is worth knowing: underscore was an ordinary identifier until Java 8, which started warning, and Java 9 made it a reserved keyword.

That freed it up, and JEP 456 made unnamed variables and patterns final in Java 22.

About this answer

Last updated
12 November 2026
Versions it names
Java 22, Java 9, Java 8, Java 21
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Use unnamed variables and patterns (Java 22) to simplify each case: an unused catch...": https://scrivio-interview-library.netlify.app/java/sealed-and-pattern-matching/72ff1fa05b/