Monday, 19 September 2016

Java 9: "var" for local variables. Improvement in Type Inference

Post Brief Table of Content

  • Introduction
  • Java 9 var Basics
  • Is var a keyword?
  • Type Inference Improvements
  • Advantages of this Feature


Before reading this post, please go through my previous Java 9 New Features posts.

In this post, we are going to discuss about "var" new identifier and some Type Inference improvement to support this new identifier.

Will update some more points soon.

Java 9 var Basics

Java 9 is going to introduce a new identifier "var", which is used to define local variables as shown below.

The main aim of this change is : Local-Variable Type Inference

This change is implementing as part of JEP 286.


Before Java 9:

-> List<> marksList = new ArrayList<Int>();  
Here we are should use either List<Int> or List<> (Diamond operator introduced in Java SE 7) to define local variables.

Here Java Compiler is able to infer only type parameter.

In Java 9:

-> var marksList = new ArrayList<Int>();  
Here we are defining just var and variable name that's it.

Here Java Compiler is able to infer whole reference type. Here Java Compiler will infer the type of marksList as ArrayList<Int> only, but not List<Int> .

Java 9 Compiler will infer the type based on the type definition available at RHS Side.

-> var runnable = new Runnable() { ... }

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

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.

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.

Type Inference Improvements

Java SE 9 is going to introduce "var" identifier to define local variables with some improvement of Type Inference.

As we discussed in the above section, Java Compiler will analyse right hand side definition and automatically infer the reference type. 

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

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 9 var identifier”. We will discuss some more Java 9 New Features Concepts in my coming posts.

Please drop me a comment if you like my post or have any issues/suggestions. I love your valuable comments so much.

No comments:

Post a Comment