web
 

Java SE 9: “var” for local variables


Oracle Corporation is going to release Java New Version: Java SE 9 around March 2017. So, I would like to deliver a series of Posts on Java SE 9 New Features. It is my third post in this series.

The var for local variables

Java SE 9 is coming with “var” to define and initialise local variables. Is it a new keyword? We will discuss it in the coming section.

The main intention of this change in the Java Language is that:

  • To improve “Type Inference”.
  • To reduce verbosity in the code
  • To write clean code

Java SE 6: local variables

Before Java SE 7, to initialise local variables we use the following approach:

Example:-


Map<String, List<Int>> phoneBook = new HashMap<String, List<Int>>();

Here we need to define type parameters at both ends. It looks like some verbose right.

Java SE 7: local variables

Java SE 7 has introduced a Diamond Operator to reduce some verbosity. It is represented as empty angle brackets: .

We can use this Diamond Operator at right hand side to avoid the declaring of same type parameters twice as shown below:

Example:-


Map<String, List<Int>> phoneBook = new HashMap<>();

Here we are reducing some verbosity by avoiding type parameters at right hand side. However, we can observe that still some verbosity is there right.

Java SE 9: local variables

To fix this verbosity issue, Java SE 9 is coming with a new feature: “var” to define and initialise the local variables as shown below:

Example-1:-


var phoneBook = new HashMap<String, List<Int>>();

Example-2:-


var list = new ArrayList<String>();  

Here Java 9 Compiler infers the type of list as ArrayList<String>, but not List<String>.

Example-3:-


var stream = list.stream(); 

Here Java 9 Compiler infers the type of list as Stream<String>.

Is var a Keyword?

In Java SE 9, “var” is NOT a keyword. It is a Reserved Type name. That means if our existing code uses var as a variable name, method name, or package name, then they do NOT effect with this change.

However any class name or interface will affect this change. It is very rare case and not recommended approach to use “var” as a class or interface name so this change does not effect existing code base.

Advantages of this improvement

Because of this new feature in Java 9, we will get the following benefits:

  • Avoid writing boilerplate code
  • Improve some Readability (Some time reduce Readability).
  • Reduce verbosity in the code.

Interview Questions

We have already discussed the following Java SE 9 Interview Questions in the above sections.

  • What is the use of Java SE 9 var?
  • Is var a keyword?
  • Will this change effect existing code base?
  • What is the main use of Java SE 9 var?
  • How Java SE 9 var improves Type Inference?

NOTE:-
They are considering val also to include into the language soon.

Like Scala supports “var” and “val”, Java 9 is also trying to include those constructs to improved Java Programming language.






journaldev is optimized for learning.© journaldev .
All Right Reserved and you agree to have read and accepted our term and condition