var
var lets you declare a local variable without writing its type, leaving the compiler to infer it from the initialiser. The variable is still statically typed and its type never changes.
Detailed Explanation
Java added var in version 10. It is not a new type and it is not dynamic typing. var count = 0; produces exactly the same bytecode as int count = 0;. The compiler works out the type from the right-hand side and then enforces it as strictly as ever. Assigning a String to that count afterwards is still a compile error.
The problem it solves is repetition. Before Java 10, declaring a nested generic type meant writing it twice, once for the variable and once for the constructor. Map<String, List<OrderLine>> byCustomer = new HashMap<String, List<OrderLine>>(); carries no information in the second half that the first half did not already have. The diamond operator fixed part of that in Java 7; var fixes the other side.
It also became necessary rather than merely convenient. Some types cannot be written down at all: the type of an anonymous class instance, an intersection type produced by a conditional, or the non-denotable types that came with pattern matching. var gives those a home.
The rules are narrow on purpose. var works only for local variables with an initialiser, for the loop variable in for and enhanced for, and for resources in try-with-resources. Since Java 11 it also works on lambda parameters, mainly so you can attach annotations to them. It does not work for fields, method parameters, return types, or catch clauses. It cannot be used with null alone, because there is nothing to infer, and it cannot be used in an array initialiser shorthand.
var is a reserved type name, not a keyword. A variable or method named var still compiles, which was deliberate so that Java 10 did not break existing code. You cannot name a class var.
The real debate is readability, and the guidance from the Java team is worth internalising. Use var when the initialiser already states the type clearly: var orders = new ArrayList<Order>(); loses nothing. Avoid it when the initialiser is a method call whose return type you cannot guess: var result = service.process(input); forces the reader into the IDE. The variable name should carry meaning when the type is hidden, so var response = client.send(request) reads better than var r = ....
There are two genuine traps. Inference picks the type of the expression, not the type you meant. var numbers = new ArrayList<Integer>() infers ArrayList<Integer>, not List<Integer>, so you have pinned the implementation into your local's type. And var x = 0 infers int, so a later assignment of a long will not compile.
The cost is entirely in review and maintenance, not performance. Overused, it makes diffs and code review on a web page harder, because reviewers do not have type hints. Used where the type is obvious, it removes noise and makes the interesting part of the line easier to see.
Code Examples
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class VarUsage {
record OrderLine(String sku, int quantity) { }
public static void main(String[] args) {
// Good: the constructor on the right states the type plainly.
var linesByCustomer = new HashMap<String, List<OrderLine>>();
linesByCustomer.put("ada", List.of(new OrderLine("kb-1", 2)));
// Inferred as Map.Entry<String, List<OrderLine>> in the loop.
for (var entry : linesByCustomer.entrySet()) {
// Inferred as int; the name carries the meaning.
var total = entry.getValue().stream().mapToInt(OrderLine::quantity).sum();
System.out.println(entry.getKey() + " ordered " + total);
// prints: ada ordered 2
}
var names = new ArrayList<String>(); // inferred ArrayList<String>, not List<String>
names.add("grace");
System.out.println(names.size()); // prints: 1
}
}public class VarLimits {
public static void main(String[] args) {
// var x; // error: cannot infer, no initialiser
// var y = null; // error: null alone gives no type
// var[] z = {1, 2, 3}; // error: var is not allowed for array types
var index = 0; // inferred int
// index = 10_000_000_000L; // error: long cannot be assigned to int
var total = 0L; // the L makes it a long; be explicit when it matters
total += 10_000_000_000L;
System.out.println(total); // prints: 10000000000
// var is a reserved type name, not a keyword: this still compiles.
var var = "legal, but do not do this";
System.out.println(var); // prints: legal, but do not do this
}
}Common Gotchas
`var` is locals only
fields, method parameters, and return types still need explicit types. The compiler rejects `private var total = 0;` outright.
Inference captures the concrete type
`var list = new ArrayList<String>()` gives you an `ArrayList<String>` local, so you can call `ensureCapacity` and you have coupled the local to that implementation. Write `List<String> list = new ArrayList<>()` when the abstraction matters.
Numeric literals infer narrowly
`var seconds = 0` is an `int`, so accumulating milliseconds into it overflows silently. Write `0L` or declare the type when the width is part of the design.
`var` does not slow anything down
the type is fully resolved at compile time and the bytecode is identical. Any argument against it is about readability, not performance.