var is still static typing - the compiler fixes the type at the declaration. It shines on long generic types and in for loops.
var byCategory = new TreeMap<String, List<String>>();
byCategory.computeIfAbsent("java", k -> new ArrayList<>()).add("streams");
byCategory.computeIfAbsent("java", k -> new ArrayList<>()).add("records");
for (var entry : byCategory.entrySet()) {
System.out.println(entry.getKey() + " -> " + entry.getValue());
}
var total = 0;
for (var i = 1; i <= 5; i++) total += i;
System.out.println("total = " + total + " (inferred int)");
java -> [streams, records]
total = 15 (inferred int)
Run this yourself in the Online Java Compiler, spin up a live REST API in the API Sandbox, or practise with Java interview questions.
Published 2026-07-30