Type inference with var
Use var for local variable type inference — less noise, same safety.
Code Comparison
✕ 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
}
Why the modern way wins
Less boilerplate
No need to repeat complex generic types on both sides of the assignment.
Better readability
Focus on variable names and values, not type declarations.
Still type-safe
The compiler infers and enforces the exact type at compile time.
Old Approach
Explicit Types
Modern Approach
var keyword
Since JDK
10
Difficulty
beginner
JDK Support
Type inference with var
Available
Widely available since JDK 10 (March 2018)
How it works
Since Java 10, the compiler infers local variable types from the right-hand side. This reduces visual noise without sacrificing type safety. Use var when the type is obvious from context.