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

Dhanji R. Prasanna dhanji at gmail.com
Thu Nov 9 22:07:57 EST 2006


A similar problem exists with parameterized collections--at runtime I can
add objects not of a reified type via reflection (unless using a checkedSet
or list..which throws CCE).
Is this true?

On 11/10/06, Joshua Bloch <josh at bloch.us> wrote:
>
> 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
> >
>
>
> _______________________________________________
> 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/20061110/12a8d2e3/attachment.html 


More information about the Concurrency-interest mailing list