Skip to content
Tutorials Center
My learning

Topics

Java Reference ยท Keywords and Statements

The var keyword

Reference for Java var: let the compiler infer local variable types, where var is allowed, where it is not, and how it affects readability and generics.

var lets the compiler infer the type of a local variable from its initializer. It was added in Java 10. The variable is still statically typed: var n = 5; is an int for the rest of its life, and var s = "hi"; is a String.

var is a reserved type name, not a regular keyword, so you can still name a variable or method var. It reduces repetition in declarations like var map = new HashMap<String, List<Integer>>();, but should not hide a type the reader cannot see.

Java
var count = 3;                          // int
var names = new ArrayList<String>();    // ArrayList<String>

Try it

Java What gets inferred

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Java Where var is not allowed

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Syntax

Java
var name = initializer;
for (var x : collection) { }
for (var i = 0; i < n; i++) { }
try (var in = open()) { }
(var a, var b) -> a + b          // lambda parameters, Java 11
Allowed Not allowed
Local variables with an initializer Fields, method parameters, return types
Loop variables, try-with-resources Variables with no initializer or with null
Implicitly typed lambda parameters Array initializers { ... }, lambdas, method references as the sole initializer
Multiple declarators (var a = 1, b = 2;) and array brackets (var a[] = ...)

Usage notes

  • Inference uses the expression's type, which can be more specific than you want: var list = new ArrayList<String>(); is an ArrayList, not a List. Write List<String> list = ... when you want the interface type.
  • Diamond pitfall: var x = new ArrayList<>(); infers ArrayList<Object>. Give the type argument explicitly.
  • Numeric literals: var n = 5 is int, var big = 5L is long. var does not widen; var f = 1; can never hold a decimal.
  • Anonymous classes: var can name an anonymous class type, so its extra members are reachable (var o = new Object() { int twice(int n) { return n * 2; } }; o.twice(4); compiles).
  • Intersection and capture types can be inferred, which is sometimes useful with wildcards.
  • Readability rule of thumb: use var when the right side states the type (new, a cast, a clear factory name). Avoid it when the result type is unclear, such as var result = service.process(data);.
  • Not a keyword: var cannot name a class, but still works as a variable, method or package name.
  • Final: final var x = 1; is allowed. Annotations also work.
  • Compiled code: var has no effect at run time; the bytecode is the same as with the explicit type.