Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Java 10's JEP 286: Local-Variable Type Inference

DZone's Guide to

Java 10's JEP 286: Local-Variable Type Inference

Java 10 will bring with it Local Variable Type Inference. What exactly is it? How can it be used? Will it be worth all the hype?

· Java Zone ·
Free Resource

Atomist automates your software deliver experience. It's how modern teams deliver modern software.

Editor's note: The code snippets in this article have been updated.

Java 10 is around the corner, with the RC Build available here. The features targetted for this release can be found here. Of all the JEPs targeted for Java 10, the one that is the most interesting and talked about by the developer community is 286: Local-Variable Type Inference.

What Is Local Variable Type Inference?

We saw, in Java 8, the Diamond operator, which allowed us to write:

List<Map> data = new ArrayList<Map>();


as:

List<Map> data = new ArrayList<>();


The type on the RHS was inferred based on the type on the LHS. This did work, albeit in anonymous inner classes — which were fixed in Java 9.

Java 10 goes a step further and says that the below:

List<Map> data = new ArrayList<>();


can be written as:

var data = new ArrayList<>();


Local variable type inferencing allows the developer to skip the type declaration associated with local variables (those defined inside method definitions, initialization blocks, for-loops, and other blocks like if-else) and the type is inferred by the JDK.

Where Can It Be Used?

Let me create a sample class to show the different ways to use a local variable inference var:

public class LegalLocalVarInferenceDemo{
 
    //in a static/instance initialization block
    static {
        var anotherName = "Sanaulla";
        System.out.println("Hello, " + anotherName);
    }
 
    public static void main(String[] args){
 
        //as a local variable
        var name = "Mohamed Sanualla";
        System.out.println("Hello " + name);
 
        var data = new ArrayList<Map>();
        data.add(Map.of("key1", "value1", "key2", "value2"));
        data.add(Map.of("key11", "value11", "key22", "value22"));
        System.out.println(data);
 
        System.out.println("********** As iteration variable in enhanced for-loop ***************");
        for ( var object : data){
            System.out.println(String.format("%s of type %s", object, object.getClass().getName()));
        }
 
        System.out.println("********** As looping index in for-loop ***************");
        for ( var i = 0 ; i < data.size(); i++ ){
            var object = data.get(i);
            System.out.println(String.format("%s of type %s", object, object.getClass().getName()));
        }
 
        System.out.println("********** As a return value from another method ***************");
        var anInteger = someAnotherMethod();
        System.out.println("someAnotherMethod returned " + anInteger);
 
    }
 
    //As a return value in a method
    public static Integer someAnotherMethod(){
        System.out.println("someAnotherMethod called");
        var returnObj = 12;
        return returnObj;
    }
 
}


Where Can't It Be Used?

Let me create another sample class which shows how var cannot be used:

public class IllegalLocalVarInferenceDemo{
    //Not permitted in class fields
    //var someProperty;
 
    //Not allowed as parameter for constructor
    // public LocalVarInferenceDemo(var param1){
 
    // }
 
    public static void main(String[] args){
 
        //Not allowed in catch parameter
        // try{
        //     //some operations
        // }catch(var ex){
 
        // }
    }
 
    //Not permitted below
    //public static void someMethod(var param1, var param2){
    //   System.out.println("Some method called");
    //}
 
    //Not permitted in method return type
    // public static var someAnotherMethod2(){
    //     System.out.println("someAnotherMethod called");
    //     var returnObj = 12;
    //     return returnObj;
    // }
}


Sample Code

This sample can be found on GitHub.

Get the open source Atomist Software Delivery Machine and start automating your delivery right there on your own laptop, today!

Topics:
java ,java 10 ,type inference ,var ,tutorial

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}