Java 10: Local Variable Type Inference

by Santosh K Saha | Category Java-10


Local-Variable Type Inference introduced in Java 10. It brings the "var" reserved type name to Java. we can now skip specifying the type for local variables and instead simply use the keyword "var".


Lets understand local variable type inference with examples


Conventional locale variable declaration and assignment in java

import java.io.File;
import java.util.ArrayList;
import java.util.stream.Stream;

public class A {
    public static void main(String[] args) {
        int a = 5;
        String b = "Ram";
        File c = new File("a.txt");
        ArrayList<String> d = new ArrayList<String>();
        Stream<String> f = d.stream();
    }
}

Using Local variable type inference

import java.io.File;
import java.util.ArrayList;

public class B {
    public static void main(String[] args) {
        var a = 5;
        var b = "Ram";
        var c = new File("a.txt");
        var d = new ArrayList<String>();
        var f = d.stream();
    }
}

What does work with local variable type inference

public class X {
}

public class Y extends X {
}

package com.stosh;

import java.util.ArrayList;

public class C {

    public static int abc(int i) {
        return i * 5;
    }

    public static void main(String[] args) {

        var a = "This is java";
        var b = a.substring(4);

        //Polymorphism  work
        var c = new X();
        c = new Y();

        //Super-type can not assign to sub-type
        var d = new Y();
//      d = new X();

        //Generic -- collection of String
        var e = new ArrayList<String>();
        e.add("Ram");
        e.add("Hari");
        String s1 = e.get(0);

        // collection of object
        var f = new ArrayList<>();
        f.add("Ram");
        f.add(true);
        String s2 = (String) f.get(0); // Cast

        //Local variable declared in a loop
        for (var x = 1; x <= 5; x++) {
            var z = x * 2;
            System.out.println(z);
        }

        //Index of Enhanced For Loop
        for (var g : e) {
            System.out.println("--> " + g);
        }

        var h = e.size() > 0 ? e.size() : -1;
        int i = h;

        var j = e.size() > 0 ? "Hari" : 1;
        //j is of type serializable need to cast
        //String k = j ;
        System.out.println("1--> " + j + "---" + j.getClass());

        //Passing to method
        var k = 5;
        k = abc(k);

    }
    
}

Limitations(What does not work?)

There are certain limitations of using var.

import java.util.function.Predicate;

public class D {

    // Can not use var for instance properties
    var name;

    public D() {
    }

    //Cannot use var for constructor parameters
    public D(var name) {
    }

    //Cannot use var for method parameters
    public void abc(var x) {
    }

    //Can not use with method return type
    public var xyz() {
        return 5;
    }


    public static void main(String[] args) {

        //Cannot use on variable without initializer
        var a;
        a = "Hari";

        //Can not ise on initializing var variable with null
        var b = null;

        //Cannot be used for compound declaration
        var c = 2, d = 4;

        //cannot reassign to a different type
        var e = 5;
        e = "Ram";

        //array initializer needs an explicit target-type
        var f = {1, 2};

        //lambda expression needs an explicit target-type
        var g = () -> {
        };

        //method reference needs an explicit target-type
        var minimum = Math::min;

        // Can not use var on catch clause
        try {

        } catch (var e) {
        }

        //Super-type can not assign to sub-type
        var d = new Y();
//      d = new X();

    }

}
Note: Like our facebook page to get updated of free classes and tutorials.