Language beginner

Enhanced for with var

Use var in for-each loops to reduce generic type noise.

✕ Java 8
List<Map.Entry<String, Integer>> list = ...;
for (Map.Entry<String, Integer> entry
        : list) {
    process(entry);
}
✓ Java 10+
var list = getEntries();
for (var entry : list) {
    process(entry);
}
👁

Less noise

Complex generic types in loops obscure the logic.

��

Same safety

Type inference is compile-time — no runtime penalty.

🔄

Refactor-friendly

Change the collection type and the loop var adjusts automatically.

Old Approach
Explicit Loop Type
Modern Approach
var in for-each
Since JDK
10
Difficulty
beginner
Enhanced for with var
Available

Widely available since JDK 10 (March 2018)

How it works

When iterating over collections with complex generic types, var in the loop variable reduces clutter while the compiler still knows and enforces the exact type.

Share 𝕏 🦋 in