Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Java generics, what does List<? super String> mean?

Tags:

java

generics

Can anyone explain how this both can compile and how it works?

List<? super String> list = new ArrayList<Object>();

As I understood it, the implementation of this needs to be either a String list or a list of objects that have String as super class? Have I missed something?

like image 749
Marthin Avatar asked May 24 '12 12:05

Marthin


2 Answers

No (i.e. yes, you have missed something :-) . <? super String> is any class which is a superclass of String (including String itself). (In this case, the only other suitable class is Object.)

What you described would be <? extends String> (which in this specific case wouldn't be very useful as String is final, so it can have no subclasses).

like image 141
Péter Török Avatar answered Nov 07 '22 15:11

Péter Török


<? super String> accepts String and any superclass.

Not to be confused with:

<? extends String> accepts String and any subclass (of which there aren't any since String is final).

like image 5
NPE Avatar answered Nov 07 '22 15:11

NPE