Sub topic 2
From identifier to error to feature: the underscore's history
This chapter assumes you know core Java syntax, lambdas, and try/catch. You don't need pattern matching yet. The question here is why _ was an ordinary name, then a compile error, and then the marker for "unused" in Java 22.
Stage 1: `_` as an ordinary identifier
In Java 1.0 through 8, _ was a legal identifier. It followed the same rules as a or count. Nothing about it meant "ignore this".
public class OldUnderscore {
public static void main(String[] args) {
int _ = 5;
System.out.println(_);
}
}This program compiles on Java 8 and prints 5. The variable is a real one, and you can read it. Few programmers wrote code like this, but the compiler allowed it.
Stage 2: a warning in Java 8
Java 8 added lambda expressions. Lambda parameters often go unused, so many developers wanted a short placeholder for them. Other languages, such as Scala, already used _ for this.
Meanwhile, Java 8 let _ stay a valid name. The Java team planned to give _ a special meaning later. If programmers kept using it as a name, that later change would break their code.
So Java 8 started the transition. Using _ as an identifier now produced a compiler warning. The warning told you the name might become a keyword in a future release.
Stage 3: an error in Java 9
Java 9 turned the warning into an error. _ became a reserved word, so it could no longer name anything.
public class NineAndLater {
public static void main(String[] args) {
int _ = 5; // error on Java 9 through 20
}
}The compiler on those versions reports that _ is a keyword and cannot be used as an identifier. The same message appeared for a lambda such as x -> ... when x was written as _.
This stage had a purpose. Once old code could no longer use _ as a name, the language could safely give it a new meaning. Code written before Java 9 that used _ as a name had to be renamed.
Stage 4: a preview in Java 21
Java 21 introduced the feature as a preview in JEP 443, "Unnamed Patterns and Variables". A preview feature is complete enough to try, but it can change. It is disabled by default. You enable it at compile time and run time.
public class PreviewDemo {
public static void main(String[] args) {
int count = 0;
for (var _ : args) {
count++;
}
System.out.println(count);
}
}On Java 21, compile this with javac --release 21 --enable-preview PreviewDemo.java and run it with java --enable-preview PreviewDemo. Without the flags, the compiler rejects the code.
Stage 5: a final feature in Java 22
JEP 456 finalized the feature in Java 22. No flags are needed now. The underscore stands for a variable you must declare but never use. It also appears in patterns, where it matches a value without naming it. The next chapters cover each use.
The same program now runs with plain javac and java on Java 22 and later:
public class FinalDemo {
public static void main(String[] args) {
int count = 0;
for (var _ : args) {
count++;
}
System.out.println("arguments: " + count);
}
}Migrating older code
Check which Java version your project targets before you use _.
| Target version | What `_` does |
|---|---|
| Java 8 or earlier | Valid name (a warning on Java 8) |
| Java 9 to 20 | Compile error |
| Java 21 | Works only with preview flags |
| Java 22 and later | Works as an unnamed variable or pattern |
Code that compiled on Java 9 or later never used _ as a name, so the new meaning cannot change what it does. Code from before Java 9 that used _ as a name already fails to compile on Java 9 and later. Rename those variables before you raise the target version.
Exercise
A project builds with Java 17. A teammate adds catch (NumberFormatException _). What happens, and what are two ways to fix it?
Show answer
The compiler rejects it, because _ is a keyword on Java 9 through 20 and unnamed variables only exist from Java 21 as a preview and Java 22 as a final feature. Fix it by renaming the parameter, for example catch (NumberFormatException e), or by moving the project to Java 22 or later.