Generics is the most powerful feature of J2SE 5. It is most useful with collections such as Set and List. Collections are bound to contain a specific type of object. It allows a Java programmer to abstract over types. Therefore, if a programmer tries to insert any element other than the specified type into the collection, a compile time error will be thrown.
For example, a method declared as
Generics has the following advantages:
- No typecasting is required when an element is taken out of a collection.
- Code is checked at the compile time rather than the runtime, so no ClassCastException occurs.
Responses to “Generics”