Home » Java » Core Java » Java 10 – JEP 286: Local-Variable Type Inference

About Mohamed Sanaulla

Mohamed Sanaulla

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

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();
        data.add(Map.of("key1", "value1", "key2", "value2"));
        data.add(Map.of("key11", "value11", "key22", "value22"));
        data.add("hello");
        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

Published on Java Code Geeks with permission by Mohamed Sanaulla, partner at our JCG program. See the original article here: Java 10 – JEP 286: Local-Variable Type Inference

Opinions expressed by Java Code Geeks contributors are their own.

(0 rating, 0 votes)
You need to be a registered member to rate this.
3 Comments Views Tweet it!
Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy

3
Leave a Reply

avatar
1 Comment threads
2 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
2 Comment authors
Mohamed SanaullaStimpy Recent comment authors

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

  Subscribe  
newest oldest most voted
Notify of
Stimpy
Guest
Stimpy

List<Map data = new ArrayList(); ?
I don't think it is possible. This code does not compile. I wonder what javacodegeeks is about to become?

Mohamed Sanaulla
Guest

Thanks for notifying. Its a typo. I will fix it on my blog post.

Mohamed Sanaulla
Guest

Actually a typo which occurred due to the blog engine stripping of the characters