According to the release notes of Java 10, it states:
We seek to improve the developer experience by reducing the ceremony
associated with writing Java code, while maintaining Java's commitment
to static type safety, by allowing developers to elide the
often-unnecessary manifest declaration of local variable types. This
feature would allow, for example, declarations such as:
var list = new ArrayList<String>(); // infers ArrayList<String>
var stream = list.stream(); // infers Stream<String>
So basically, it's to accommodate the laziness of programmers; instead of writing List<String> list = new ArrayList<>();
, you can now omit the List<String>
, and replace it with var
, thus allowing Java to infer which type list
is - it's able to do this because the variable is initialized when it's defined. Note, you'll have to infer the type of the list
when initializing (in the case above, you'd add String
to new ArrayList<String>();
). Thus, Java still keeps its static type declaration.
What is the difference to Kotlin?
The type of a variable in Kotlin defined with var
is also inferred (but can also be explicit in case of var foo: Int
), and Kotlin is also a statically typed language.