Home core java Generalized target type inference - java 8

Generalized target type inference – java 8

1. What is type inference ?

Invoking a generic method or constructor without specifying a type between angle brackets is called type inference. Type inference is a Java compiler’s ability to look at each method invocation and corresponding declaration to determine the type argument that make the invocation applicable.

To understand target-type inference in java 8 more clearly let’s see history of generics.

Generics in Java :

To provide type safety and to resolve typecasting issues the generics was introduced in Java 5, that introduced type parameters for classes and methods.

For example to create Araylist in java 5 the following approach is used. Here Integer is type parameter that should specify both sides.

- Advertisement -

List<Integer> list = new ArrayList<Integer>();  

In java 7 this was improved as below. The diamond operator <> introduced. The compiler can infer parameter types for constructors of generic classes. Here compiler has ability infer parameter type without providing type parameter in angle brackets. This is called type inference.

List<Integer> list = new ArrayList<>();

But the compiler could not figure out the generic types for a method invocation. see following example.

2. Target-Type inference in Java 7 example :

public class Java7TypeInferenceDemo {
 
    public static void main(String[] args) {
        
        Java7TypeInferenceDemo demo = new Java7TypeInferenceDemo();
 
        demo.genericMethod(new HashMap<Integer, String>());
        demo.genericMethod(new HashMap<>());//Compiler Error
 
    }
    
    public void genericMethod(Map<Integer, String> map) {
        //Some operatios
        System.out.println(map);
    }
}

Above example if you compile against java 1.7 the following line give the compilation error. Compiler could not figure out the generic types for a method invocation in java 7.

demo.genericMethod(new HashMap<>());//Compiler Error

3. Target-Type inference in Java 8 example :

Java 8 added support generalized target-type inference in method context, so we can now remove the specific generic parameter type in method invocations. See below example, run this example in java 1.8, you wont get any compiler error.

public class Java8TypeInferenceDemo {
 
    public static void main(String[] args) {
        
        Java8TypeInferenceDemo demo = new Java8TypeInferenceDemo();
 
        demo.genericMethod(new HashMap<Integer, String>());
        demo.genericMethod(new HashMap<>());//No Compiler Error
 
    }
    
    public void genericMethod(Map<Integer, String> map) {
        //Some operatios
        System.out.println(map);
    }
}

References

  1. JEP 101
- Advertisement -
Satish Varma
Satish Varmahttps://javabydeveloper.com
Satish is post graduated in master of computer applications and experienced software engineer with focus on Spring, JPA, REST, TDD and web development. Also founder of javabydeveloper.com. Follow him on LinkedIn or Twitter or Facebook

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Stay in Touch

Categories

Related Articles