wrapper classes advantages - Generic
Java collections only store Objects, not primitive types; however we can store the wrapper classes.
- Java primitive types are not reference types (e.g. an intis not anObject)
- Java does generics using type-erasure of reference types (e.g. a Listis really aListat run-time)
Since both of these are true, generic Java collections can not store primitive types directly. For convenience, autoboxing is introduced to allow primitive types to be automatically boxed as reference types. Make no mistake about it, though, the collections are still storing object references regardless.
Could this have been avoided? Perhaps.
- If an intis anObject, then there's no need for box types at all.
- If generics aren't done using type-erasure, then primitives could've been used for type parameters.
 
 
Comments
Post a Comment