Sub topic 11
Java versions, preview history, and migrating older code
This chapter assumes you have read the earlier chapters on unnamed variables and patterns. It also assumes you can compile and run Java from the command line. It covers which JDK you need, what happened to _ before it became a feature, and how to move existing code over.
How the underscore changed over time
In early Java, _ was an ordinary identifier, so int _ = 5; compiled. Java 8 began to warn about it. Java 9 made _ a keyword, and using it as a name became a compile error. That reserved the symbol for a later feature.
Java 21 delivered that feature as a preview (JEP 443). Java 22 made it final (JEP 456).
What a preview feature means for you
A preview feature is complete and specified, but it can still change before it is final. The compiler and runtime refuse to use it unless you opt in.
On JDK 21, you must enable preview at both compile time and run time:
javac --release 21 --enable-preview Demo.java
java --enable-preview DemoWithout the flag, JDK 21 rejects _ and reports that unnamed variables are a preview feature. The classes you compile with preview enabled are marked as depending on preview features. They run only on the same JDK feature release they were built with. A class built this way on JDK 21 will not load on JDK 22.
On JDK 22 or later, no flag is needed:
javac --release 22 Demo.java
java Demo| JDK | Status of `_` | Flags needed |
|---|---|---|
| 8 | Legal identifier, with a warning | None |
| 9 to 20 | Error | Not available |
| 21 | Preview | `--enable-preview` and `--release 21` at compile time, `--enable-preview` at run time |
| 22 and later | Final | None |
Migrating older code
Migration has two parts. The first is code that uses _ as a real name, which is rare. This code cannot compile on Java 9 or later, so you must rename it before you can move past Java 8. The other part is code that already avoids the problem with placeholder names such as ignored, unused, or ignore. You can change these to _ when you want to, but nothing forces you to.
Here is an older file with several placeholder names:
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
class Legacy {
static int parseOrDefault(String text, int fallback) {
try {
return Integer.parseInt(text);
} catch (NumberFormatException ignored) {
return fallback;
}
}
static int count(List<String> items) {
int total = 0;
for (String unused : items) {
total++;
}
return total;
}
static Map<String, List<Integer>> group(List<String> keys, List<Integer> values) {
Map<String, List<Integer>> groups = new HashMap<>();
for (int i = 0; i < keys.size(); i++) {
groups.computeIfAbsent(keys.get(i), k -> new ArrayList<>()).add(values.get(i));
}
return groups;
}
public static void main(String[] args) {
System.out.println(parseOrDefault("x", -1));
System.out.println(count(List.of("a", "b")));
System.out.println(group(List.of("a", "a"), List.of(1, 2)));
}
}The migrated version changes only the three unused names:
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
class Modern {
static int parseOrDefault(String text, int fallback) {
try {
return Integer.parseInt(text);
} catch (NumberFormatException _) {
return fallback;
}
}
static int count(List<String> items) {
int total = 0;
for (var _ : items) {
total++;
}
return total;
}
static Map<String, List<Integer>> group(List<String> keys, List<Integer> values) {
Map<String, List<Integer>> groups = new HashMap<>();
for (int i = 0; i < keys.size(); i++) {
groups.computeIfAbsent(keys.get(i), _ -> new ArrayList<>()).add(values.get(i));
}
return groups;
}
public static void main(String[] args) {
System.out.println(parseOrDefault("x", -1));
System.out.println(count(List.of("a", "b")));
System.out.println(group(List.of("a", "a"), List.of(1, 2)));
}
}Both files print the same output. The migration changes how the code reads, not what it does.
A safe migration checklist
- Raise the build's Java level to 22 or later. In Maven that is
maven.compiler.release, and in Gradle it is the toolchain's language version. - Search for any identifier named exactly
_and rename it. The compiler will report these. - Check that your tools understand the new syntax. Old versions of IDEs, linters, formatters, and code-coverage agents may fail to parse
_in places where it is now legal. - Convert placeholder names gradually. A team-wide rule such as "use
_for unused catch parameters and lambda parameters" keeps the codebase consistent. - Do not publish libraries compiled with
--enable-previewon JDK 21. Consumers on any other JDK cannot load them.
Exercise
Your team builds on JDK 21 and wants to use catch (IOException _). Name two things you must do, and one risk you take on.
Show answer
Pass --enable-preview together with --release 21 to javac, and pass --enable-preview to the java launcher at run time. The risk is that the compiled classes are tied to preview on JDK 21, so they will not load on JDK 22 or any other release. Moving the build to JDK 22 or later removes both the flags and the risk.