Java Reference ยท Types and Literals
Java type inference
Reference for type inference in Java: local variable var, the diamond operator, lambda parameter inference and generic method inference, with limits.
Java is statically typed, but the compiler can work out many types for you. Type inference means you leave a type out and the compiler fills it in from the surrounding code. The inferred type is fixed at compile time; nothing is looked up at run time.
Java infers types in four main places: local variables declared with var (since Java 10), the diamond operator <> on new expressions (since Java 7, extended to anonymous classes in 9), lambda parameters, and the type arguments of generic method calls.
var names = new ArrayList<String>(); // ArrayList<String> List<String> copy = new ArrayList<>(); // diamond: ArrayList<String> BiFunction<Integer, Integer, Integer> add = (a, b) -> a + b;
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.
Rules
| Feature | Since | Where it works |
|---|---|---|
var x = expr; |
Java 10 | Local variables with an initializer, for and for-each variables, try-with-resources variables. |
(var a, var b) -> ... |
Java 11 | Implicitly typed lambda parameters; all or none must use var. |
new Box<>(...) |
Java 7 | Any new of a generic class; anonymous classes since Java 9. |
(a, b) -> ... |
Java 8 | Lambda parameter types come from the target functional interface. |
Collections.emptyList() |
Java 5/8 | Type arguments of generic method calls come from arguments and the assignment target. |
Usage notes
varis not dynamic typing. The variable has one static type for its whole life, and the compiler checks it exactly as if you had written the type.varis a reserved type name, not a keyword. You can still name a variablevar, but you cannot name a classvar.- Where
varis not allowed: fields, method parameters, return types,catchparameters and variables without an initializer. - Readability. Use
varwhen the type is obvious from the right-hand side (newor a clearly named factory). Avoid it when it hides a meaningful type, such asvar result = service.process();. - Primitive types stay primitive.
var n = 1;isint, notInteger. Use an explicit type if you want a wider one (long,double). - Diamond with
varloses information.var list = new ArrayList<>();infersArrayList<Object>. Put the type argument on the right:new ArrayList<String>(). - Intersection and anonymous types.
varcan hold types you cannot write out, such as an anonymous class instance whose extra members stay accessible. - Wildcard capture.
varon a wildcard-typed expression infers the captured type, which is sometimes useful for helper code but surprising when printed.
Related
- var, Generics syntax, Lambda expressions, Reference types
- Lessons: var, Generics