Why Collection is made generic ?

Prior to jdk1.5 the collection could store any type of Objecct. Which means that collection can store any type of object. Thus it was possible to accidentally store incompatible types in collection. Doing so could result in run-time mismatch errors. With generics it is possible to explicitly state the type of data being stored and runtime mismatch errors can be avoided. Finally generics is added for type safety.
In pre generic when you retrieve the object from the collection you must manually cast that reference into proper type.

No comments:

Post a Comment