I have introduced to you about Type Inference in Java in the previous tutorial. If you have read it, you will probably know that this is a feature of the Java Compiler based on how the declaration, the call to determine the type of data without having to explicitly declare, reducing the code redundancy. From Java 10 onwards, Java introduces the var keyword apply Type Inference, help us no need to explicitly declare the data type of local variables in a method or code block.
For example, from Java 9 and earlier, when declaring a List variable of a String object, we will declare the following:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
package com.huongdanjava.javaexample; import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class Example { public static void main(String[] args) { List<List<String>> names = new ArrayList<List<String>>(); names.add(Arrays.asList("Khanh", "Thanh", "Tan")); names.forEach(l -> l.forEach(System.out::println)); } } |
Result:
But from Java 10 onwards, you do not need to declare the type of data in front of the variable names anymore, just declare with the keyword var is.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
package com.huongdanjava.javaexample; import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class Example { public static void main(String[] args) { var names = new ArrayList<List<String>>(); names.add(Arrays.asList("Khanh", "Thanh", "Tan")); names.forEach(l -> l.forEach(System.out::println)); } } |
The result is the same:
Determining the data type of a local variable will be based on the code that we declared. In the above example, because in the right side, we have declared new an ArrayList object, so Java Compiler will understand that this is a List variable.
If before, the variable names were declared using the diamond operator (also a Type Inference) from Java 7:
then you cannot use the var keyword here. Because at this time, the Java Compiler only has the Type Inference names variable to the Object data type, but to the Object object, we cannot use methods of the ArrayList object.
As I said, we can also use the var keyword in the block code as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
package com.huongdanjava.javaexample; import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class Example { public static void main(String[] args) { var names = new ArrayList<List<String>>(); names.add(Arrays.asList("Khanh", "Thanh", "Tan")); for (var name : names) { for (var n : name) { System.out.println(n); } } } } |
Result:
Or:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
package com.huongdanjava.javaexample; public class Example { { var i = 5; System.out.println(i); } public static void main(String[] args) { Example e = new Example(); } } |
Result:
You should understand: although we can use the var keyword when writing code, but when compiling these code, the data type will be compiled to exactly the data type that we want. So, when you declare this type of data, you cannot assign it to another data type.
Eg: