Unnamed variables with _
Use _ to signal intent when a variable is intentionally unused.
Porównanie kodu
✕ 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);
});
Widzisz problem z tym kodem? Daj nam znać.
Dlaczego nowoczesne podejście wygrywa
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.
Stare podejście
Unused Variable
Nowoczesne podejście
_ Placeholder
Od JDK
22
Poziom trudności
Początkujący
Wsparcie JDK
Unnamed variables with _
Dostępne
Finalized in JDK 22 (JEP 456, March 2024).
Jak to działa
Unnamed variables communicate to readers and tools that a value is deliberately ignored. No more 'ignored' or 'unused' naming conventions, no more IDE warnings.
Powiązana dokumentacja