List<?> is not equivalent to List<? extends Object>

Rémi Forax forax at
Mon Apr 21 12:35:49 PDT 2008

Tom Hawtin a écrit :
> Rémi Forax wrote:
>> It's not a big problem in general, if you want a List<?>, you declare 
>> a List<?>
>> But (there is always a but), when the compiler infers a type argument
>> it's more problematic, by example,
>> HashSet<Class<?>> constantClassSet=new HashSet<Class<?>>();
>> Collections.addAll(constantClassSet,
>>  String.class, Object.class
>> );
>> emits a warning because the second line creates an array of
>> Class<? extends Object> which is not safe.
>> I think it's stupid, i propose to modify section (JLS3) to
>> add a line saying that if an inferred type argument is Type<? extends 
>> Bound>
>> with Bound the bound of the type varaible of Type, it is inferred 
>> Type<?>.
> Would that be useful in general?
not in general. Just for that use case.
> Consider if you had used String.class and Integer.class. Then the 
> inferred component type would be Class<? extends 
> Serializable&Comparable<? extends Serializable&Comparable<?>> (or 
> something like that!). So your rule wouldn't come in to play.
For the records, Maurizio point me that it's can be seen as a specific 
case of that bug:
> Tom Hawtin
> (Disclaimer: I am not a language lawyer.)

(Disclaimer: so am i)

More information about the compiler-dev mailing list