GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • java.evolved
  • โ†
  • โ†’

This view

  • Original page
  • New address

GeezerVison is showing javaevolved.dev โ€” Unnamed variables with _ | java.evolved

Home / Language / Unnamed variables with _
Language Beginner

Unnamed variables with _

  • ๐Ÿ› Report a code issue
  • ๐ŸŒ Report a translation issue
  • ๐Ÿ’ก Suggest a new pattern

Use _ to signal intent when a variable is intentionally unused.

Pattern Matching
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);
});
Share ๐•
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 warnings

IDEs and linters won't flag intentionally unused variables.

๐Ÿงน

Cleaner lambdas

Multi-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
Unnamed Variables & Patterns (JEP 456) โ†—
Proof
View proof source โ†—
Related patterns
Language Beginner

Type inference with var

Java 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
}
Hover to see modern โžœ
JDK 10+ โ†’
Language Beginner

Text blocks for multiline strings

Java 8
String json = "{\n" +
    "  \"name\": \"Duke\",\n" +
    "  \"age\": 30\n" +
    "}";
Java 15+
String json = """
    {
      "name": "Duke",
      "age": 30
    }""";
Hover to see modern โžœ
JDK 15+ โ†’
Language Beginner

Switch expressions

Java 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";
};
Hover to see modern โžœ
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.