Java SE 9: “var” for local variables

Filed Under: Java

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


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>>();

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


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

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.

Comments

  1. Sai Krishna Sahu says:

    In your examples, List is used but we cannot use Int and it should be Integer

  2. Anbu says:

    JEP 286 is it made in JDK 9 ??

    1. Rambabu says:

      No. They are still working on this.

  3. Valentino says:

    Hi Rambabu.
    I’ve downloaded java 9, but I do not get the var feature working. For example, if I write in JShell the instruction

    var list = new ArrayList();

    I get the following. Can you help, please?

    jshell> var list = new ArrayList();
    | Error:
    | cannot find symbol
    | symbol: class var
    | var list = new ArrayList();
    | ^-^

    1. Rambabu says:

      Thanks for reading my tutorials. If you go thru the whole post, you will get this note.
      Oracle Corp is still working on this feature. They have not decided yet about releasing this feature in Java 9 or next release. We will get updates soon.
      Apart from var, they are also looking to use val.

      Ram

      1. James Moore says:

        It has, in fact, been decided. See the JEP at http://openjdk.java.net/jeps/286 – it will NOT be making it into Java 9. Java 9 has, in fact, been released since September 21st. No additional features are added after a release. (Nevermind that it was marked feature complete December of 2016, when JEP-286 was moved to Java 10)

        1. Stephan Roch says:

          It is included in Java 10.

          1. Rambabu says:

            Yes right, initial they had plan as part of JDK 9, then moved to JDK 10. Thats why we have another post. Please check that new post.

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages