import java.util.*;
class Demo {
void example() {
var name = "Alice"; // String
var n = 42; // int
var users = new HashMap<String, List<Integer>>(); // no diamond noise
// The right-hand side dictates the type — both are correct
Map<String, List<Integer>> longForm = new HashMap<>();
var shortForm = new HashMap<String, List<Integer>>();
// In for-loops
for (var entry : users.entrySet()) { // Map.Entry<String, List<Integer>>
System.out.println(entry.getKey() + " = " + entry.getValue());
}
// ✗ var is ONLY for local variables — not for fields, params, returns
// ✗ var without initializer is a compile error
// ✗ var = null is a compile error (no type to infer)
}
}
Create a free account and build your private vault. Share publicly whenever you want.