Local Variable Type Inference or LVTI in Java 10

What is type inference?

Type inference refers to the automatic detection of the datatype of a variable, done generally at the compiler time.

What is Local Variable type inference?

Local variable type inference is a feature in Java 10 that 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. It will, then, be the job of the compiler to figure out the datatype of the variable.

Why has this feature been introduced?



Till Java 9, to define a local variables of class type, the following was the only correct syntax:

Class_name variable_name=new Class_name(arguments);

For example:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Sample Java local variable declaration
import java.util.ArrayList;
import java.util.List;
class A {
    public static void main(String a[])
    {
        List<Map> data = new ArrayList<>();
    }
}

chevron_right


Or

filter_none

edit
close

play_arrow

link
brightness_4
code

class A {
    public static void main(String a[])
    {
        String s = " Hi there";
    }
}

chevron_right


It looks fine, right? Yeah, because this is how things have been since the inception of Java. But there is one issue: It’s pretty obvious that if the type of the object is clearly mentioned at the right side of the expression, mentioning the same thing before the name of the variable makes it redundant. Plus, in the second example, you can see that it’s obvious that after the ‘=’ sign, it’s clearly a string as nothing except for a string can be enclosed in double inverted commas. Therefore, there arose a need to eliminate this redundancy and make variable declaration shorter, and more convenient.

How to declare local variables using LVTI:

Instead of mentioning the variable datatype on the left-side, before the variable, LVTI allows you to simply put the keyword ‘var’. For example,

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code for Normal local 
// variable declaration
import java.util.ArrayList;
import java.util.List;
class A {
    public static void main(String ap[])
    {
        List<Map> data = new ArrayList<>();
    }
}

chevron_right


Can be re-written as:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code for local variable 
// declaration using LVTI
import java.util.ArrayList;
import java.util.List;
class A {
    public static void main(String ap[])
    {
        var data = new ArrayList<>();
    }
}

chevron_right


Use Cases

Here are the cases where you can declare variables using LVTI:

  1. In a static/instance initialization block
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Declaration of variables in static/init 
    // block using LVTI in Java 10
    class A {
        static
        {
            var x = "Hi there";
            System.out.println(x)'
        }
        public static void main(String[] ax)
        {
        }
    }

    chevron_right

    
    

        Output:
         Oh hi there
    
  2. As a local variable
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Declaration of a local variable in java 10 using LVTI
    class A {
        public static void main(String a[])
        {
            var x = "Hi there";
            System.out.println(x)
        }
    }

    chevron_right

    
    

          Output:
          Hi there
    

  3. As iteration variable in enhanced for-loop
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Declaring iteration variables in enhanced for loops using LVTI in Java
    class A {
        public static void main(String a[])
        {
            int[] arr = new int[3];
            arr = { 1, 2, 3 };
            for (var x : arr)
                System.out.println(x + "\n");
        }
    }

    chevron_right

    
    

    Output:
    1
    2
    3
    

  4. As looping index in for-loop
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Declaring index variables in for loops using LVTI in Java
    class A {
        public static void main(String a[])
        {
            int[] arr = new int[3];
            arr = { 1, 2, 3 };
            for (var x = 0; x < 3; x++)
                System.out.println(arr[x] + "\n");
        }
    }

    chevron_right

    
    

    Output:
    1
    2
    3
    

  5. As a return value from another method
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Storing the return value of a function in a variable declared with LVTI
    class A {
        int ret()
        {
            return 1;
        }
        public static void main(String a[])
        {
            var x = new A().ret();
            System.out.println(x);
        }

    chevron_right

    
    

    Output:
    1
    

  6. As a return value in a method
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Using a variable declared 
    //using the keyword 'var' as a return value of a function
    class A {
        int ret()
        {
            var x = 1;
            return x;
        }
        public static void main(String a[])
        {
            System.out.println(new A().ret());
        }
    }

    chevron_right

    
    

    Output:
    1
    

Error cases:
There are cases where declaration of local variables using the keyword ‘var’ produces an error. They’re mentioned below:

  1. Not permitted in class fields
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Sample java code to demonstrate 
    //that declaring class variables 
    //using 'var' is not permitted
    class A {
        var x; /* Error: class variables can't be declared
                using 'var'. Datatype needs
                 to be explicitly mentioned*/
    }

    chevron_right

    
    

  2. Not permitted for uninitialized local variables
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Sample java code to demonstrate 
    //that declaring uninitialized 
    //local variables using 'var' produces an error
    class A {
        public static void main(String a[])
        {
            var x; /* error: cannot use 'var' 
                    on variable without initializer*/
        }
    }

    chevron_right

    
    

  3. Not allowed as parameter for any methods
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java code to demonstrate that
    // var can't be used in case of 
    //any method parameters
    class A {
        void show(var a) /*Error: can't use 'var'
                           on method parameters*/
        {
        }
    }

    chevron_right

    
    

  4. Not permitted in method return type
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java code to demonstrate
    // that a method return type
    // can't be 'var'
    class A {
        public var show() /* Error: Method return type 
                                         can't be var*/
        {
            return 1;
        }
    }

    chevron_right

    
    

  5. Not permitted with variable initialized with ‘NULL’
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java code to demonstrate that local 
    variables initialized with 'Null' 
    can't be declared using 'var'*/
    class A {
        public static void main(String a[])
        {
            var x = NULL; // Error: variable initializer is 'null'
        }

    chevron_right

    
    

Note: All these pieces of code run only on Java 10.



My Personal Notes arrow_drop_up

Intern Technical Content Writing at GeeksforGeeks

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.