Java SE 9: "var" for local variables with Examples

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.

I have already delivered few posts on Java SE 9 New Features. Before going through this posts, please read them below :

In this post, we are going to discuss one more Java SE 9 New Feature: “var” with some simple and suitable examples.

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:-

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:-

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:-

Here Java 9 Compiler with infer the type of phoneBook reference as new HashMap<String, List<Int>>.

When we use var identifier like this to define local variables, compiler will infer it’s type automatically.

NOTE:-
The type is inferred based on the type of the initializer. If there is no initializer, the initializer is the null literal, or the type of the initializer is not one that can be normalized to a suitable denotable type.

Example-2:-

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

Example-3:-

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

NOTE:- Oracle Corp. is still working on this feature and they have not finalised about release of this feature in Java SE 9 or in future releases. We will wait for their updates.

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.

However, Scala uses:

  • var to define variables or mutable data
  • val to define values or immutable data

That’s it all about “Java SE 9: var for local variables” concept. We will discuss some more Java SE 9 New Features in my coming posts.

Please drop me a comment if you like my post or have any issues/suggestions/type errors.

Thank you for reading my tutorials.

Happy Java SE 9 Learning!

NOTE: This feature was moved out of Java 9 and it’s part of Java 10 now. Please head over to the updated article at Java 10 Local Variable Type Inference.

By admin

Leave a Reply

%d bloggers like this: