GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing javaevolved.dev โ Unnamed variables with _ | java.evolved
Language
Beginner
Unnamed variables with _Use _ to signal intent when a variable is intentionally unused. Code Comparison
โ Java 8
try {
parse(input);
} catch (Exception ignored) {
log("parse failed");
}
map.forEach((key, value) -> {
process(value); // key unused
});
โ Java 22+
try {
parse(input);
} catch (Exception _) {
log("parse failed");
}
map.forEach((_, value) -> {
process(value);
});
See a problem with this code? Let us know.
Why the modern way wins
๐ข
Clear intent_ explicitly says 'this value is not needed here'. ๐
No warningsIDEs and linters won't flag intentionally unused variables. ๐งน
Cleaner lambdasMulti-param lambdas are cleaner when you only need some params. Old Approach
Unused Variable
Modern Approach
_ Placeholder
Since JDK
22
Difficulty
Beginner
JDK Support
Unnamed variables with _
Available
Finalized in JDK 22 (JEP 456, March 2024). How it works
Unnamed variables communicate to readers and tools that a value is deliberately ignored. No more 'ignored' or 'unused' naming conventions, no more IDE warnings. Related Documentation
Proof
Related patterns
Language
Beginner
Type inference with varJava 8
Map<String, List<Integer>> map =
new HashMap<String, List<Integer>>();
for (Map.Entry<String, List<Integer>> e
: map.entrySet()) {
// verbose type noise
}
Java 10+
var map = new HashMap<String, List<Integer>>();
for (var entry : map.entrySet()) {
// clean and readable
}
JDK 10+
โ
Language
Beginner
Text blocks for multiline stringsJava 8
String json = "{\n" +
" \"name\": \"Duke\",\n" +
" \"age\": 30\n" +
"}";
Java 15+
String json = """
{
"name": "Duke",
"age": 30
}""";
JDK 15+
โ
Language
Beginner
Switch expressionsJava 8
String msg;
switch (day) {
case MONDAY:
msg = "Start";
break;
case FRIDAY:
msg = "End";
break;
default:
msg = "Mid";
}
Java 14+
String msg = switch (day) {
case MONDAY -> "Start";
case FRIDAY -> "End";
default -> "Mid";
};
JDK 14+
โ
|
| You are visitor 01402503 โ GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800ร600. No cookies required. |