Java Type Inference

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<Integer> list = new ArrayList<Integer>();

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<Integer> 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(List<Integer>list){
  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<Integer> list1 = new ArrayList<Integer>();
  12.         list1.add(11);
  13.         showList(list1);
  14.         // Java 7  
  15.         List<Integer> 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<X> {
  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<String> x){
  10.         x.setName(“John”);
  11.         returnx.name;
  12.       }
  13. }
  14. public class TypeInferenceExample {
  15.     public static void main(String[] args) {
  16.         GenericClass<String> genericClass = new GenericClass<String>();
  17.         genericClass.setName(“Peter”);
  18.         System.out.println(genericClass.getName());
  19.         GenericClass<String> genericClass2 = new GenericClass<>();
  20.         genericClass2.setName(“peter”);
  21.         System.out.println(genericClass2.getName());
  22.         // New improved type inference
  23.         System.out.println(genericClass2.genericMethod(new GenericClass<>()));
  24.     }
  25. }

Output:

Peter
peter
John
Next TopicJava 8 method parameter reflection

 

java for beginners
java for beginners
java for beginners
CodSir //course // books
...

WikiSir

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 […]

Tags:

....
...