Array was poor man's generics.
Java could get by just fine if it only allows Object[]
. You can get by just fine if you limit yourself to Object[]
only. But just like the non-generic List
, using it is kind of painful. Programmer needs some other ways to remind himself what type of objects an array stores.
So Java allows you to specify the type of the elements in an array, like Number[]
, similar to generified List<Number>
. Now javac can make sure you don't insert wrong type of elements; and you don't have to downcast an element retrieved from an array; and there's a simple covariances (for example, Interger[]
is subtype of Number[]
)
Therefore, before the introduction of real generics, array is superior to List
in APIs. It's better to return a Number[]
than a raw List
; the latter is hard to use for callers.
Now, with real generics, these benefits of arary are no longer relavant. Almost anywhere in implementation you needed an X[]
, you can use an ArrayList<X>
instead; anywhere in AP you needed an X[]
, you can use a List<X>
instead.
When you do need to use array, like when programming ArrayList<T>
itself, or some other basic data structures, It is ok to use Object[]
only. A generic array won't help you much, while being quite confusing itself.