Search Results

Search found 1 results on 1 pages for 'user1111929'.

Page 1/1 | 1 

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

    - by user1111929
    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?

    Read the article

1