I was just looking at the method defined in the List interface:
, and I have a question. Why is it generic? Because of that fact, m
It is type-safe -- it doesn't cause a ClassCastException. That's generally what type-safe means.
ArrayStoreException is different. If you include ArrayStoreException in "not type-safe", then all arrays in Java are not type-safe.
The code that you posted also produces ArrayStoreException. Just try:
TestGenerics
In fact, it is simply not possible to allow the user to pass in an array of the type they want to get, and at the same time not have ArrayStoreException. Because any method signature that accepts an array of some type also allows arrays of subtypes.
So since it is not possible to avoid ArrayStoreException, why not make it as generic as possible? So that the user can use an array of some unrelated type if they somehow know that all the elements will be instances of that type?