Generics is the implementation of parametric polymorphism.
Using parametric polymorphism, a method or data type can be written
generically so that it can deal equally well with objects of various
types. It is a way to make a language more expressible, while still
maintaining full static type-safety.
Why use generics?
There are mainly two reasons to use generics. These are:
- Performance – Collections that store objects use Boxing and Unboxing
on data types. This uses a significant amount of overhead, which can
give a performance hit. By using generics instead, this performance hit
is removed.
- Type safety – There is no strong type information at compile time as to what is stored in the collection.
No comments:
Post a Comment