Sub topic 2
From identifier to error to feature: the underscore's history
This chapter assumes you read the previous chapter on why unused variables get in the way. It also assumes you can read basic Java: local variables, catch blocks, lambdas and records. You do not need to know patterns in detail yet. They come later.
The underscore went through three stages in Java: a legal name, a reserved word you could not use, and finally a feature with its own meaning. This history explains three things you will meet in real code. Old source files may fail to compile on modern Java. Some error messages mention _ and a version number. And the _ feature is final in Java 22 but only a preview feature in Java 21.
Stage 1: `_` as an ordinary identifier (Java 1.0 to Java 7)
In the first versions of Java, _ was a normal identifier. The language treated it like x or count. Java identifiers may contain letters, digits, _ and $, and nothing in the rules said a name could not consist of the underscore alone.
The following program is legal in Java 8 and earlier.
public class OldStyle {
public static void main(String[] args) {
int _ = 41;
_ = _ + 1;
System.out.println(_);
}
}Here _ is a real variable. It has a value, you can read it, and you can assign to it. It is a poor name, but the compiler accepted it. In practice, few programmers used it. Some people used it as a placeholder name for variables they did not care about, which looked like conventions from other languages. The compiler treated it as a normal variable, so two declarations of _ in the same scope conflicted just like two declarations of count.
This is the key point for the rest of the history. Because _ was a valid name, Java could not simply give it a new meaning. Doing so would silently change what existing programs did, or break them.
Stage 2: warning in Java 8, error in Java 9
Language designers had other languages in front of them. Scala, Kotlin, C#, Rust, Haskell and Python all use _ to mean "a value I am ignoring". Java developers asked for the same thing, especially for lambdas, where a parameter is often required by the functional interface but not needed by the body.
Before Java could add the feature, it had to clear the name. This happened in two steps.
- Java 8 added a warning. Using
_as an identifier still compiled, butjavacprinted a warning saying that the use of_as an identifier might not be supported in later releases. This was advance notice. - Java 9 made it an error. From Java 9 on,
_became a keyword. Code that declared a variable, parameter, method or class named_stopped compiling. The message said, roughly, that as of release 9_is a keyword and may not be used as an identifier.
Java 8 and 9 behaved differently on the same file. Here is how the program from Stage 1 behaves on each release.
| Compiler | `int _ = 41;` |
|---|---|
| Java 7 | Compiles |
| Java 8 | Compiles, with a warning |
| Java 9 to Java 20 | Compile error: `_` is a keyword |
Between Java 9 and Java 20, _ was reserved but had no meaning. You could not use it as a name, and you could not use it for anything else either. The reservation was deliberate. By removing it from the set of legal names first, the designers could later give it a meaning without changing what any valid program meant. A program that was legal in Java 9 never contained _ at all, so no legal program could change behavior when the feature arrived.
Around Java 9, there was an idea to use _ for unused lambda parameters. That idea was written up as a draft proposal known as "lambda leftovers". It was not delivered then. The designers wanted a more complete design that covered locals, catch parameters and patterns together, rather than only lambdas. That is why the final feature has several parts.
What the reservation looked like for developers
If you ever upgrade a legacy codebase from Java 8, you may see errors like the one below.
import java.util.function.Function;
public class LegacyLambda {
public static void main(String[] args) {
Function<String, Integer> length = _ -> 0;
System.out.println(length.apply("abc"));
}
}On Java 8 this compiles, and _ is a lambda parameter named _ with a warning. On Java 9 through 20 it fails, and the compiler tells you to pick another name. On Java 22 and later it compiles again, but now for a different reason: _ no longer names a parameter at all. It declares an unnamed lambda parameter. The program text is the same in Java 8 and Java 22, yet the meaning differs. The result of this particular program is the same in both cases, but that is by luck. Code that read the parameter would not survive the change. The next section shows the contrast.
Stage 3: `_` as a feature (Java 21 preview, Java 22 final)
Once _ was reserved, the feature could come back with a new meaning. It arrived in two releases.
- Java 21 included it as a preview feature (JEP 443, "Unnamed Patterns and Variables"). A preview feature is fully specified and implemented but can change or be removed, so you have to opt in with
--enable-preview. - Java 22 made it a standard feature (JEP 456). No flag is needed.
In Java 21 you compile and run like this:
javac --release 21 --enable-preview Demo.java
java --enable-preview DemoIn Java 22 and later, you compile with plain javac Demo.java and run with java Demo.
Without the flag, Java 21 refuses the code and mentions that unnamed variables are a preview feature that is disabled by default. This is a common confusion. If you copy a snippet from this tutorial and run it on Java 21 without --enable-preview, you get that message. The fix is the flag or a newer JDK, not a change to the code.
The final design has three pieces, each covered in its own later chapter:
- An unnamed variable: a local variable, loop variable, resource, catch parameter or lambda parameter declared with
_as its name. - An unnamed pattern variable: a type pattern such as
Circle _. - The unnamed pattern: a bare
_inside a record pattern, which matches any component without a type or a name.
Here is a complete program that works on Java 22 and later. It shows that the meaning of _ is different from the old one.
public class UnnamedDemo {
static int next(String text) {
return Integer.parseInt(text);
}
public static void main(String[] args) {
int _ = next("1");
int _ = next("2");
System.out.println("both calls ran");
}
}Two points stand out. First, the same name _ is declared twice in one scope without a conflict. That would have been illegal when _ was an identifier, because both declarations would have created a variable with the same name. Now neither declaration creates a variable that anyone can refer to. Second, the initializer is still required, and it still runs. The right-hand side next("1") executes for its side effects even though its result is thrown away. An unnamed local variable does not skip the work. It only skips the name.
Because the variable has no name, you cannot read it afterwards. This line fails to compile on Java 22:
System.out.println(_);The compiler reports that _ can only be used to declare unnamed patterns, local variables, exception parameters or lambda parameters. Since _ was a keyword in Java 9 through 20 and is still not an expression in Java 22, reading it was never allowed after Java 8.
What did and did not change
A few details trip up readers who know the old rules.
- Only the single underscore is special. Names such as
__,_xandx_were never reserved and are still ordinary identifiers. Only the one-character name_was affected. - Java 8 code using
_as a real variable must be renamed. If the code reads the variable, there is no automatic fix. Rename it to a meaningful name. If the code never reads the variable, you can often leave_in place once you move to Java 22, but this changes the meaning, so check each case. - Release flags matter. If a build uses
--release 8with a Java 22 compiler, the older rules apply. The source level decides whether_is an identifier, an error or a feature. - Tools lag behind. Older versions of IDEs, linters and code formatters may flag
_as an error even though Java 22 accepts it. If your tools report a syntax error on valid_code, check the language level in the project settings and update the tool.
The table below summarizes each stage.
| Java version | Status of `_` | What the compiler does |
|---|---|---|
| 1.0 to 7 | Ordinary identifier | Accepts it as a name |
| 8 | Identifier, discouraged | Accepts it with a warning |
| 9 to 20 | Reserved keyword | Rejects it as an identifier |
| 21 | Unnamed variables and patterns, preview | Accepts it only with `--enable-preview` |
| 22 and later | Unnamed variables and patterns, standard | Accepts it in the allowed positions |
The whole timeline
The diagram shows the same sequence as a timeline.
Why the slow path was worth it
The long delay can look like wasted time, but it follows a consistent rule of the language: do not change the meaning of existing valid programs. A new keyword can break old code, so Java normally avoids taking over a word already in use. For _, the designers first made the old use illegal. They then waited for several releases before assigning the new meaning. This gave library and application authors time to rename their variables. The preview step in Java 21 then let real users try the feature and report problems before it became permanent.
The same pattern appears in other Java features. A change goes through a warning, then a restriction, then a preview, then a final release. When you see a feature that needs --enable-preview, you know it is complete enough to try but may still change.
Exercises
Exercise
A file contains the line int _ = compute(); and later uses _ in the expression _ + 1. It compiled on Java 8 with a warning. What happens on Java 9, and what happens on Java 22? What should you change in the file?
Show answer
On Java 9 it fails, because _ is a keyword and cannot be an identifier. On Java 22 it also fails. The declaration is now valid as an unnamed variable, but _ + 1 tries to read it, and an unnamed variable has no name to read. The fix is to rename the variable to a meaningful name, such as result, and use that name in both places.
Exercise
You paste a Java 22 snippet containing catch (NumberFormatException _) into a project that builds on Java 21, and the compiler says unnamed variables are a preview feature. Give two ways to fix this and say which one you would choose for production code.
Show answer
One way is to compile and run with --enable-preview (and --release 21). The other is to upgrade the JDK to 22 or later, where the feature is standard. For production code, upgrade the JDK, because preview features can change between releases and class files compiled with preview enabled only run on the exact same Java version.