Home » Java Type Inference for Generics

Java Type Inference for Generics

by Online Tutorials Library

Type Inference for Generic Instance Creation

In Java 7, Java provides improved compiler which is enough smart to infer the type of generic instance. Now, you can replace the type arguments with an empty set of type parameters (<>). This pair of angle brackets is informally called the diamond.

The following approach is used in Java 6 and prior version.

Now, you can use the following new approach introduced in Java 7.


Type Inference for Generic Instance Creation Example

Output:

12 12 

Type Inference and Generic Constructors

You can create generic constructor in both generic and non-generic classes. In the following example, we have created a generic type constructor.

Output:

Hello 

You may also like