- Java primitive types are not reference types (e.g. an
int
is not anObject
) - Java does generics using type-erasure of reference types (e.g. a
List
is really aList
at 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
int
is 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.
No comments:
Post a Comment