Odd warning

I read in generics that "? Extends Object" and "?" are synonymous, why is this happening.

List list=new ArrayList();
List<? extends Object> list2=list;     //1
List<?> list3=list;                    //2

      

For 1 unchecked warning, warnings are selected, but not for 2. So the compiler somewhere is definitely distinguishing between the two. Plz explains the difference between the two compared to the above code

+3


source to share


2 answers


I read in generics that "? Extends Object" and "?" are synonymous

Not really. The first template has a bottom border, the second does not. It shouldn't matter for your two examples (well, except that you can only add null

in list2

and list3

!).

This lower bound could mean "erasure signature" (I don't know the exact term).

The best example for this is Collections.max()

; you will notice that the type of the parameter is defined as T extends Object & Comparable<? super T>

.



This is because before Java 5 this method existed and was defined as:

static Object max(Collection coll)

      

If the type parameter was defined as T extends Comparable<? super T>

, this would mean that the method in 1.4 would have to return Comparable

!

+2


source


Because any type information is erased at compile time, not all types are available at run time. Types that are fully available at runtime are known as reifiable types (see http://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.7 ).



So, according to the JLS List<?>

, it is a type that can be re-identified but is List<? extends Object>

not, which means they are not the same from the compiler's point of view.

0


source







All Articles