New Java feature: can data types be thrown away?
A long time ago, when writing code, we should carefully consider the data types of variables, such as the following:
However, when it comes to JDK 10, we have a new choice. The function of VaR local variable inference is added in JDK 10. Using it, we can forget the data type. How is it used? Next, let's take a look.
1. Use comparison
Next, we will use the method of comparison to experience the role of var.
Scenario 1: define string
Old writing:
String str = "Hello,Java.";
New writing:
var s = "Hello,Java.";
Scenario 2: numerical addition
Old writing:
int num1 = 111;
double num2 = 555.666d;
double num3 = num1 + num2;
System.out.println(num3);
New writing:
var n1 = 111L;
var n2 = 555.666;
var n3 = n1 + n2;
System.out.println(n3);
Scenario 3: Collection
Old writing:
List<Object> list = new ArrayList<>();
list.add("Hello");
list.add("Java");
New writing:
var list = new ArrayList<>();
list.add("Hello");
list.add("Java");
Scenario 4: cycle
Old writing:
for (Object item : list) {
System.out.println("item:" + item);
}
for (int i = 0; i < 10; i++) {
// do something...
}
New writing:
for (var item : list) {
System.out.println("item:" + item);
}
for (var i = 0; i < 10; i++) {
// do something...
}
Scenario 5: used with lambda
Old writing:
List<Object> flist = list.stream().filter(v ->
v.equals("Java")).collect(Collectors.toList());
System.out.println(flist);
New writing:
var flist = list.stream().filter(v ->
v.equals("Java")).collect(Collectors.toList());
System.out.println(flist);
2. Advantage analysis
From the above example, we can see that VAR has two obvious advantages: improved code readability and naming alignment.
① Improved readability
Before using VaR, if the type name is very long, the following situation will occur:
InternationalCustomerOrderProcessor<AnonymousCustomer,SimpleOrder<Book>> orderProcessor =
createInternationalOrderProcessor(customer,order);
When each line is limited to 150 characters, the variable name will be pushed to the next line, so that the readability of the whole code becomes very low. But when we use VaR, the code becomes like this:
var orderProcessor = createInternationalOrderProcessor(customer,order);
As can be seen from the above code, the longer the type, the greater the value of VaR (readability).
② Named alignment
When VaR is not used, the code is as follows:
// 显式类型
No no = new No();
AmountIncrease<BigDecimal> more = new BigDecimalAmountIncrease();
HorizontalConnection<LinePosition,LinePosition> jumping =
new HorizontalLinePositionConnection();
Variable variable = new Constant(6);
List<String> names = List.of("Java","中文社群");
After using VaR, the code is as follows:
var no = new No();
var more = new BigDecimalAmountIncrease();
var jumping = new HorizontalLinePositionConnection();
var variable = new Constant(6);
var names = List.of("Java","中文社群");
From the above code, we can see that after using VaR, the naming is aligned, and the whole code becomes more elegant.
3. Usage rules & counterexamples
The implementation of VaR comes from JEP 286 (improvement proposal 286). The detailed address is: http://openjdk.java.net/jeps/286
It can be seen from the title "inference of local variable type" of JEP 286 that VaR can only be used for local variable declaration, that is, VAR must meet the following conditions:
Counterexample 1: uninitialized and null assignment
Counterexample 2: midway type change
Counterexample 3: global variables
Counterexample 4: as the return value
4. Principle analysis
After the previous use, we have a preliminary understanding of VaR, but what is the implementation principle of VaR?
In order to understand its principle, we compiled the following code (using the command javac maintest. Java):