In this recipe, you will learn about local-variable type inference, which was introduced in Java 10, where it can be used, and its limitations.
Using local-variable type inference
Getting ready
A local-variable type inference is the ability of a compiler to identify the type of the local variable using the correct side of an expression. In Java, a local variable with an inferred type is declared using the var identifier. For example:
var i = 42; //int
var s = "42"; //String
var list1 = new ArrayList(); //ArrayList of Objects;
var list2 = new ArrayList<String>(); //ArrayList of Strings
The type of each of the preceding variables is clearly identifiable. We captured their types in comments.
...