On March 21, Oracle announced the official release of Java 10. This is the first official release of Java after the big release cycle. Java 10: Local variable Type inference Java 10: Local variable Type Inference Java 10: Local variable Type inference

Since Java 10 came out, many articles have been published telling us what features are available and how local variable type inference is used. But, know why, know why.

As soon as Java 10 was released, I downloaded the Jdk and installed it on my computer, wrote a piece of code and tried decomcompiling it to really get a feel for how local variable inference works. This article will briefly talk about my feelings.

The principle of

The use of local variable type inference is covered in my Java 10 release this month, Which will Change the Way you Write Code. It can be used in the following scenarios:

Public class VarDemo {public static void main(String[] args) {var String = "hollis"; Var stringList = new ArrayList<String>(); stringList.add("hollis"); stringList.add("chuang"); stringList.add("weChat:hollis"); stringList.add("blog:http://www.hollischuang.com"); For (var s: stringList){system.out.println (s); } // The local variable in a traditional for loop is defined for (var I = 0; i < stringList.size(); i++){ System.out.println(stringList.get(i)); }}}Copy the code

Then, compile using Java 10’s javac command:

/Library/Java/JavaVirtualMachines/jdk-10.jdk/Contents/Home/bin/javac VarDemo.java
Copy the code

Generate vardemo. class file, we decompile vardemo. class. Decompile jad to get the following code:

public class VarDemo { public static void main(String args[]) { String s = "hollis"; ArrayList arraylist = new ArrayList(); arraylist.add("hollis"); arraylist.add("chuang"); arraylist.add("weChat:hollis"); arraylist.add("blog:http://www.hollischuang.com"); String s1; for(Iterator iterator = arraylist.iterator(); iterator.hasNext(); System.out.println(s1)) s1 = (String)iterator.next(); for(int i = 0; i < arraylist.size(); i++) System.out.println((String)arraylist.get(i)); }}Copy the code

This code is familiar from before Java 10, when there was no native variable type inference. The corresponding code is as follows:

Local variable type inference writing Normal writing
var string = “hollis”; String string = “hollis”;
var stringList = new ArrayList(); ArrayList stringList = new ArrayList();
for (var s : stringList) for (String s : stringList)
for (var i = 0; i < stringList.size(); i++) for (int i = 0; i < stringList.size(); i++)

ArrayList arraylist = new ArrayList(); ArrayList stringList = new ArrayList(); Type erasure after deicing.

for(Iterator iterator = arraylist.iterator(); iterator.hasNext(); System.out.println(s1)) is a form of for (String s: String list).

So, local variable type inference is the syntactic sugar that Java 10 provides to developers. Although var is defined in the code, it is not recognized by the virtual machine. When Java files are compiled into class files, the real type of the variable is used instead of var (e.g. String String instead of var String). For virtual machines, there is no need to make any compatibility changes to VAR because its life cycle ends at compile time. The only change is that the compiler needs to add an extra sugar fix to var during compilation.

Interested students can write two pieces of code, one using var and one without var, and compare the compiled bytecode. You’ll find it’s really exactly the same. Below is my comparison with the Diff tool.

Syntactic Sugar, also known as sugar-coated grammar, is a term coined by British computer scientist Peter.J.Landin to describe a grammar added to a computer language that has no effect on how the language functions, but is more convenient for programmers to use. In short, syntactic sugar makes programs simpler and more readable.

What’s the difference with JavaScript

As many of you know, in JavaScript, variable definitions are declared using var. So, as soon as Java 10’s local variable type inference came out, someone said, “Isn’t that JavaScript?” This is the same as var in JS.

Actually, it’s really different.

First, JavaScript is a weakly typed (or dynamically typed) language, meaning that the type of a variable is indeterminate. You can use a syntax like “5-4” in JavaScript, and the result is the number 1, where the string and the number are being manipulated. If you don’t believe me, open your browser’s console and try:

However, While you can use var to declare variables in Java, it is still a strongly typed language. As you can see from the decomcompiled code above, var is just a syntactic sugar provided by Java to developers. Ultimately, after compilation, the object type defined by var must be defined as the type inferred by the compiler.

Will not affect readability local variable type inference are the most let a person of readability, because before, we define a variable to explicitly specify the type of the he, so when reading the code just to see the kinds of statement can know his type, but after all use var, it’ll be up shit creek. No doubt some of the readability is lost. However, using var to declare objects in code also brings many benefits, such as simpler code.

Before a new thing comes out, there are always all kinds of unaccustomed. Now people are going to think that this stuff is too inefficient for me to read the code. Like Taobao Mall just changed its name to Tmall, we all think, what a ghost name. Now listen to the habit, do not feel very good.

If everyone uses var to declare variables, the name of the variable becomes even more important. At that point, people will pay more attention to the readability of variable names. Moreover, it is believed that the major IDE will introduce the intelligent display variable inferred type function soon. So, from all aspects, can make up for some deficiencies.

In general, I embrace the feature of local variable type inference.

thinking

Finally, one more question for you to consider is that local variable type inference seems to work pretty well, and now that Java has decided to roll it out in a new version, why restrict its use? The only known scenarios in which you can declare variables using var are initializing local variables, enhancing indexes for for loops, and local variable definitions for traditional for loops. There are also several scenarios in which this usage is not supported, such as:

Method parameters

Arguments to the constructor

Method return value type

Object that is a member variable

So, my question is, why does Java make these restrictions, and what are the considerations? Discussions and solutions to these questions will also be held on knowledge Planet.

Pay attention to my public number, 2000G Java learning resources for free