Why does Java ArrayList use per-element casting instead of per-array casting?

前端 未结 4 667
梦如初夏
梦如初夏 2021-01-07 18:47

What happens inside Java\'s ArrayList (and probably many other classes) is that there is an internal Object[] array = new Object[n];, to w

相关标签:
4条回答
  • 2021-01-07 19:00

    Whenever an element is read from it, a cast return (T) array[i]; is done. So, a cast on every single read.

    Generic is a compile time check. At runtime the type T extends is used instead. In this case T implicitly extends Object so what you have at runtime is effectively.

    return (Object) array[i];
    

    or

    return array[i];
    

    Wouldn't it be more logical and also slightly faster to just create a

    T[] array = (T[]) new Object[n]
    

    Not really. Again at runtime this becomes

    Object[] array = (Object[]) new Object[n];
    

    or

    Object[] array = new Object[n];
    

    What you are really fishing for is

    T[] array = new T[n];
    

    except this doesn't compile, mostly because T isn't known at runtime.

    What you can do is

    private final Class<T> tClass; // must be passed in the constructor
    
    T[] array = (T[]) Array.newInstance(tClass, n);
    

    only then will the array actually be the type expected. This could make reads faster, but at the cost of writes. The main benefit would be a fail fast check, i.e. you would stop a collection being corrupted rather than wait until you detect it was corrupted to throw an Exception.

    0 讨论(0)
  • 2021-01-07 19:05

    I think is more a matter of code style not of performance or type safety (because the backing array is private)

    The java 5 ArrayList was implemented the way you suggested with an E[] array. If you look at the source code you see that it contains 7 (E[]) casts. From java 6 the ArrayList changed to use an Object[] array which resulted in only 3 (E) casts.

    0 讨论(0)
  • 2021-01-07 19:20

    It's more complicated than that: generics are erased in byte code, and the erasure of T[] is Object[]. Likewise, the return value of get() becomes Object. To retain integrity of the type system, a checked cast is inserted when the class is actually used, i.e.

    Integer i = list.get(0);
    

    will be erased to

    Integer i = (Integer) list.get(0);
    

    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:

    T[] array = Array.newInstance(tClass, n);
    

    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:

    Object[] array = new String[1];
    array[0] = 1; // throws ArrayStoreException
    
    ArrayList list = new ArrayList<String>();
    list.add(1); // causes heap pollution
    

    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.

    0 讨论(0)
  • 2021-01-07 19:21

    Array is object too. Here T[] array = (T[]) new Object[n] you cast only (T[]) object type not elements in the array.

    0 讨论(0)
提交回复
热议问题