Quick revision
The short answer
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.
What to remember
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.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
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.
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.
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.
| Place | Before | Java 22 and later |
|---|---|---|
| catch parameter | catch (NumberFormatException e) | catch (NumberFormatException _) |
| lambda parameter | (region, v) -> total.add(v) | (_, v) -> total.add(v) |
| record component | Point(var x, var y) | Point(var x, _) |
| for loop variable | for (Order o : orders) count++ | for (Order _ : orders) count++ |
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.
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.
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.
- Official documentation
- The Java Language Specification, Java SE 21, Java SE 21 API documentation
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/