The Generics Framework, introduced in Java SE 5.0 and updated in Java SE 7 and 8, provides support that allows for the parameterization of types. Generics over Primitive Types is targeted for Java SE 10.
The benefit of generics is the significant reduction in the amount of code that needs to be written when developing a library. Another benefit is the elimination of casting in many situations.
The classes of the Collections Framework, the class
Class, and other Java libraries have been updated to include generics.
See Java Generics and Collections by Philip Wadler and Maurice Naftalin (O’Reilly, 2009) for comprehensive coverage of the Generics Framework.
Generic classes and interfaces parameterize types by adding a type parameter within angular brackets (i.e.,
<T>). The type is instantiated at the place of the brackets.
Once instantiated, the generic parameter type is applied throughout the class for methods that have the same type specified. In the following example, the
get() methods use the parameterized type as their parameter argument and return types, respectively:
When a variable of a parameterized type is declared, a concrete type (i.e.,
<Integer>) is specified to be used in place of the type parameter (i.e.,
Subsequently, the need to cast when retrieving elements from things such as collections would ...