Local Variable Type Inference is one of the important noticeable changes to language obtainable from Java 10 beyond. It permits you to explain a variable by making use of var and without defining its type. The assembler infers the type of the variable making use of the value given. This type of inference is confined to local variables.
The old way of declaring local variables
String name = "Welcome to intellinuts.com";
The new way of declaring local Variable
var name = "Welcome to intellinuts.com";
Now the assembler infers the type of name variable as String by checking the value given.
Map<Integer, String> mapNames = new HashMap<>(); var mapNames1 = new HashMap<Integer, String>();
Example
Use of Local Variable Type Inference in JAVA 10 is shown by the below-given example
import java.util.List; public class Tester { public static void main(String[] args) { var names = List.of("Julie", "Robert", "Chris", "Joseph"); for (var name : names) { System.out.println(name); } System.out.println(""); for (var i = 0; i < names.size(); i++) { System.out.println(names.get(i)); } } }
Output
Your outcome must look like this:
Julie Robert Chris Joseph Julie Robert Chris Joseph