[concurrency-interest] [HS] why creation of array of parametrized type is unsafe

Joshua Bloch josh at bloch.us
Thu Nov 9 15:09:52 EST 2006


Rémi,

Because the the array that is created is generally *not* an array of T (the
type parameter), but of its bound.  Automatically generated casts at the
call site can fail.  Run this program (which compiles with a warning) and
you'll get the idea.  The program was written by Peter van der Ahe:



public class GenericVarargs {
   static <T> T[] m1(T t1, T t2) {
       return m2(t1, t2);
   }
   static <T> T[] m2(T... args) {
       return args;
   }
   public static void main(String... args) {
       String[] strings = m1("bad", "karma");
   }
}



On 11/9/06, Rémi Forax <forax at univ-mlv.fr> wrote:
>
> It's a totally HS question, but i know that some Java gurus listen that
> list.
>
> It's perhaps a stupid question, but i don't see why array of
> parametrized type
> is unsafe.
>
> see my blog on that :
> http://weblogs.java.net/blog/forax/archive/2006/11/why_array_of_pa.html
>
> Rémi
>
> _______________________________________________
> Concurrency-interest mailing list
> Concurrency-interest at altair.cs.oswego.edu
> http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: /pipermail/attachments/20061109/15a157d9/attachment.html 


More information about the Concurrency-interest mailing list