Java Type Inference

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

Java Type Inference

Type inference is a feature of Java which provides ability to compiler to look at each method invocation and corresponding declaration to determine the type of arguments.

Java provides improved version of type inference in Java 8. the following example explains, how we can use type inference in our code:

Here, we are creating arraylist by mentioning integer type explicitly at both side. The following approach is used earlier versions of Java.

  1. List list = new ArrayList();  

In the following declaration, we are mentioning type of arraylist at one side. This approach was introduce in Java 7. Here, you can left second side as blank diamond and compiler will infer type of it by type of reference variable.

  1. List list2 = new ArrayList<>();   

Improved Type Inference

In Java 8, you can call specialized method without explicitly mentioning of type of arguments.

  1. showList(new ArrayList<>());  

Java Type Inference Example

You can use type inference with generic classes and methods.

  1. import java.util.ArrayList;  
  2. import java.util.List;  
  3. public class TypeInferenceExample {  
  4.     public static void showList(Listlist){  
  5.         if(!list.isEmpty()){  
  6.             list.forEach(System.out::println);  
  7.         }else System.out.println("list is empty");  
  8.     }  
  9.     public static void main(String[] args) {  
  10.         // An old approach(prior to Java 7) to create a list  
  11.         List list1 = new ArrayList();  
  12.         list1.add(11);  
  13.         showList(list1);  
  14.         // Java 7    
  15.         List list2 = new ArrayList<>(); // You can left it blank, compiler can infer type  
  16.         list2.add(12);  
  17.         showList(list2);  
  18.         // Compiler infers type of ArrayList, in Java 8  
  19.         showList(new ArrayList<>());  
  20.     }  
  21. }  

Output:

11
12
list is empty

You can also create your own custom generic class and methods. In the following example, we are creating our own generic class and method.


Java Type Inference Example 2

  1. class GenericClass {  
  2.       X name;  
  3.     public void setName(X name){  
  4.         this.name = name;  
  5.       }  
  6.     public X getName(){  
  7.         returnname;  
  8.       }  
  9.     public String genericMethod(GenericClass x){  
  10.         x.setName("John");  
  11.         returnx.name;  
  12.       }  
  13. }  
  14.   
  15. public class TypeInferenceExample {  
  16.     public static void main(String[] args) {  
  17.         GenericClass genericClass = new GenericClass();  
  18.         genericClass.setName("Peter");  
  19.         System.out.println(genericClass.getName());  
  20.           
  21.         GenericClass genericClass2 = new GenericClass<>();  
  22.         genericClass2.setName("peter");  
  23.         System.out.println(genericClass2.getName());  
  24.       
  25.         // New improved type inference  
  26.         System.out.println(genericClass2.genericMethod(new GenericClass<>()));  
  27.     }  
  28. }  

Output:

Peter
peter
John