Skip to content
Tutorials Center
My learning

Topics

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.

Java
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

Java var and the diamond operator

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 Generic methods, lambdas and what var cannot do

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

  • var is 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.
  • var is a reserved type name, not a keyword. You can still name a variable var, but you cannot name a class var.
  • Where var is not allowed: fields, method parameters, return types, catch parameters and variables without an initializer.
  • Readability. Use var when the type is obvious from the right-hand side (new or a clearly named factory). Avoid it when it hides a meaningful type, such as var result = service.process();.
  • Primitive types stay primitive. var n = 1; is int, not Integer. Use an explicit type if you want a wider one (long, double).
  • Diamond with var loses information. var list = new ArrayList<>(); infers ArrayList<Object>. Put the type argument on the right: new ArrayList<String>().
  • Intersection and anonymous types. var can hold types you cannot write out, such as an anonymous class instance whose extra members stay accessible.
  • Wildcard capture. var on a wildcard-typed expression infers the captured type, which is sometimes useful for helper code but surprising when printed.