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.
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.
Improved Type Inference
In Java 8, you can call specialized method without explicitly mentioning of type of arguments.
Java Type Inference Example
You can use type inference with generic classes and methods.
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
Peter peter John
JavaTpoint offers Test Series on eLitmas, Amcat, Company Placement Papers, Programming Languages, SSC, Banking, Civil Services etc.