Posted by alexfromsun on May 5, 2011 at 11:37 AM PDT
Generics doesn't work well with arrays. Everybody knows that you can't create a generified array,
but not many people really know what it was done this way.
A nice article from Brian Goetz helped me to understand the problem
when I studied the new features of JDK 5.
The arrays in java are covariant when generics are not. The following code clearly shows it:
String stringArray = new String; // compiles ok Object objectArray = stringArray; // throws ArrayStoreException objectArray = new Object();
If a generfied program is compiled without a warning you can safely add an Object to your ArrayList of Objects.
However when there is an array of Objects passed to your method as a parameter, there is no way to understand an object of what type you can put there
(I am not sure if you can find it out via reflection).
So it is known that arrays and generics don't do well together. It means that it is not recommended to use two features of JDK 5 together,
I am talking about generics and varargs.
This reminds me one old bug found by R