The Generics Framework, introduced in Java SE 5.0, provides support that allows for the parameterization of types.
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 Maurice Naftalin and Philip Wadler (O’Reilly, 2006) 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
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
<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 be eliminated:
// Collection List/ArrayList with Generics