Java
Here, we’ll discuss the significance of Var variable in Java 10 and learn how to use Java Var variable in software projects.
As many of you might have heard, Java10 was released in March 2018. It’s a short-term release from Oracle Corporation and came with a lot of new features or enhancements. One of the important features of Java10 is local variable type inference. The upcoming Java release will be in September 2018. It will be a long-term support (LTS) version of Java. Let’s look at a Java10 local variable type inference feature with an example now.
String str=new String("Java 10"); Integer integer=new Integer(10);
The main advantage of this feature is to increase code readability. Here’s an example:
A developer would have no problem reading the above two statements. In another example, here are some more complex statements that are kind of a pain to write:
Map<String,String> map=new HashMap<String,String>(); Map<User,List<String>> listofMovies=new HashMap<>();
In Java10, the var keyword allows local variable type inference, which means the type for the local variable will be converted by the compiler, so you don’t need to declare that type. Hence, you can replace the above two statements as below:
var map=new HashMap<String,String>(); var listofMovies=new HashMap<User,List<String>>();
Below are the few points to remember when you declaring local variable type inference in Java 10:
1. Each statement containing the var keyword has a static type which is the declared automatically type of value. This means that assigning a value of a different type will always fail. So, Java is still a statically typed language and there should be enough information about the type of a local variable. If that's not there, compilation fails, for example:
var id=0;// At this moment, compiler interprets //variable id as integer. id="34"; // This will result in compilation error //because of incompatible types: java.lang.String //can't be converted to int.
2. Let’s look at an inheritance scenario. Assume there are 2 subclasses (Doctor, Engineer) extended from the parent class Person. Let’s say someone creates an object of Doctor, as shown below:
var p=new Doctor(); // In this case, what should be //the type of p; it is Doctor or Person?
Note that in such cases, a variable declared with var is always the type of the initializer, and var may not be used when there is no initializer. Therefore, if you designate the on top of variable p, as shown below, compilation fails:
p=new Engineer(); // Compilation error saying
//incompatible types
3. Illegal Use of var
As mentioned earlier, var won't work without the initializer:
var n; // error: cannot use 'var' on variable without initializer Nor would it work if initialized with null: var emptyList = null; // error: variable initializer is 'null' It won't work for non-local variables: var p = (String s) -> s.length() > 10; // error: lambda expression needs an explicit target-type Same is the case with the array initializer: var arr = { 1, 2, 3 }; // error: array initializer needs an explicit target-type
4. Conclusion
In this blog, we see the new Java 10 local variable type inference feature with examples.
We are an ERP development company that nurtures a team of experienced Java developers that are capable of building high-quality software applications that are easy to scale. Our custom ERP software solutions enable enterprises to streamline their inbound/outbound processes, enhance productivity, and improve operational efficiency. For project-related queries, drop us a line at [email protected].