What happens inside Java’s ArrayList<T> (and probably many other classes) is that there is an internal Object[] array = new Object[n];, to which T Objects are written. Whenever an element is read from it, a cast return (T) array[i]; is done. So, a cast on every single read.
I wonder why this is done. To me, it seems like they’re just doing unnecessary casts. Wouldn’t it be more logical and also slightly faster to just create a T[] array = (T[]) new Object[n]; and then just return array[i]; without cast? This is only one cast per array creation, which is usually far less than the number of reads.
Why is their method to be preferred? I fail to see why my idea isn’t strictly better?
It’s more complicated than that: generics are erased in byte code, and the erasure of
T[]isObject[]. Likewise, the return value ofget()becomesObject. To retain integrity of the type system, a checked cast is inserted when the class is actually used, i.e.will be erased to
That being the case, any type check within ArrayList is redundant. But it’s really beside the point, because both
(T)and(T[])are unchecked casts, and incur no runtime overhead.One could write a checked ArrayList that does:
This would prevent heap pollution, but at the price of a redundant type check (you can not suppress the synthentic cast in calling code). It would also require the caller to provide the ArrayList with the class object of the element type, which clutters its api and makes it harder to use in generic code.
Edit: Why is generic array creation forbidden?
One problem is that arrays are checked, while generics are unchecked. That is:
Therefore, the component type of an array matters. I assume this is why the designers of the Java language require us to be explicit about which component type to use.