It's a lower-bounded wildcard.
Wildcards are useful in situations where only partial knowledge about the type parameter is required. [...] An upper bound is signified by the syntax:
? extends B
where B
is the upper bound. [...] it is permissible to declare lower bounds on a wildcard, using the syntax:
? super B
where B
is a lower bound.
A List<? super Integer>
, for example, includes List<Integer>
, List<Number>
, and List<Object>
.
Wildcards are used to make generics more powerful and flexible; bounds are used to maintain type safety.
See also
As to how this is useful in <T extends Comparable<? super T>>
, it's when you have something like Cat extends Animal implements Comparable<Animal>
.
Look at the signature of Collections.sort
public static <T extends Comparable<? super T>> void sort(List<T> list)
Therefore, with a List<Cat> listOfCat
, you can now Collections.sort(listOfCat)
.
Had it been declared as follows:
public static <T extends Comparable<T>> void sort(List<T> list)
then you'd have to have Cat implements Comparable<Cat>
to use sort
. By using the ? super T
bounded wildcard, Collections.sort
becomes more flexible.
See also
- Effective Java 2nd Edition, Item 28: Use bounded wildcards to increase API flexibility
- Also, PECS principle: "producer
extends
consumer super
"