Java Generics WildCard: <? extends Number> vs <T extends Number>
There is no difference in this case, because T
is never used again.
The reason for declaring a T
is so that you can refer to it again, thus binding two parameter types, or a return type together.
The difference is you can't refer to T
when using a wildcard.
You aren't right now, so there is "no difference", but here's how you could use T
to make a difference:
static <T extends Number> T getElement(List<T> l) {
for (T t : l) {
if (some condition)
return t;
}
return null;
}
This will return the same type as whatever is passed in. eg these will both compile:
Integer x = getElement(integerList);
Float y = getElement(floatList);
T
is a bounded type, i.e. whatever type you use, you have to stick to that particular type which extends Number
, e.g. if you pass a Double
type to a list, you cannot pass it a Short
type as T
is of type Double
and the list is already bounded by that type. In contrast, if you use ?
(wildcard
), you can use "any" type that extends Number
(add both Short
and Double
to that list).