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.
var count = 3; // int var names = new ArrayList<String>(); // ArrayList<String>
Try it
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.
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
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 anArrayList, not aList. WriteList<String> list = ...when you want the interface type. - Diamond pitfall:
var x = new ArrayList<>();infersArrayList<Object>. Give the type argument explicitly. - Numeric literals:
var n = 5isint,var big = 5Lislong.vardoes not widen;var f = 1;can never hold a decimal. - Anonymous classes:
varcan 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
varwhen the right side states the type (new, a cast, a clear factory name). Avoid it when the result type is unclear, such asvar result = service.process(data);. - Not a keyword:
varcannot 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:
varhas no effect at run time; the bytecode is the same as with the explicit type.