Java

Java 10 – JEP 286: Local-Variable Type Inference

Java 10 is around the corner with RC Build available here. The features targetted for this release can be found here. Of all the JEPs targetted for Java 10, the interesting and most talked about by the developer community is the 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();

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

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

List<Map> data = new ArrayList();

can be written as:

var data = new ArrayList<Map>();

Note: We need to provide the generic type information on the RHS, otherwise it will result in an ArrayList of Object.

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 it can be used?

Let me create a sample class to show the different ways to use 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 it cannot 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

Advertisements

Categories: Java, Java 10

1 reply »

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.