Highlighting the New Java Local Variable Type Inference Feature Coming in Java 10

Back in 2016, a new JDK Enhancement Proposal (JEP) was making waves in the Java community: JEP 286. Now, 2 years later, Local Variable Type Inference is arguably the most noteworthy feature coming in Java 10. This is one more step that the Java Language developers are taking in order to simplify the writing of Java applications.

Breaking Down Local Variable Type Inference
This new feature will add some syntactic sugar to Java – simplifying it and improving developer experience. The new syntax will reduce the verbosity associated with writing Java, while maintaining the commitment to static type safety.

In other words, you’ll be able to declare variables without having to specify the associated type. A declaration such as:

List list = new ArrayList();
Stream stream = getStream();

Will be replaced with this new, simplified syntax:

var list = new ArrayList();
var stream = getStream();

As you can see, the Local Variable Type Inference will introduce the use of the ‘var’ keyword rather than requiring the explicit specification of the type of the variable.

Java is known to be a bit verbose, which can be good when it comes to understanding what you or another developer had in mind when a function was written. But, for those of you who think developing software in Java is too tedious, this feature marks a significant change.

While Type Inference is not a new concept in Java, it is a new concept for local variables.

It all started in Java 5 when Generics were introduced. Then, it was developed further in Java 7 (as part of Project Coin) with the diamond operator (<>), which allows initializing lists without a type bound ArrayList<>, and in Java 8 with Lambda Formals. Using the diamond operator allowed writing the following code:

List list = new LinkedList<>();

Now, the Oracle Team is taking it one step further.

Community Response
Back before JEP 286 was, in fact, a JEP… A survey was conducted by Oracle to get a better understanding of how the Java community felt about the proposal. For the most part, the survey focused on the overall feelings toward the proposal, and how the community thought it should be implemented. Of the 2,453 developers that replied, the results were mostly positive:

The second part of the survey focused on the potential syntactic choices, suggesting 5 options to choose from based on similar use in other languages such as C#, Scala, Swift, C++ or to use ‘let’.

How Will This Affect Your Code?
Once we get all of the excitement over a new feature out of our systems, the first question we usually want to ask ourselves is: How will this affect my code? In the feature summary, “the degree of boilerplate coding required in Java” is addressed as a main motivation, so we can look forward to omitting manifest type declarations in the future.

More specifically, the treatment will be restricted to:

-Local variables with initializers
-Indexes in the enhanced for-loop
-Locals declared in a traditional for-loop

The Java team at Oracle states that it will not be available for:

-Method parameters
-Constructor parameters
-Method return types
-Catch formals (or any other kind of variable declaration)
Due to Java’s commitment to support previous versions of Java, we can assume it won’t break backwards compatibility.


java training in chennai

java official

Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account log in.