JavaGenericsWildCard: < ? 扩展数字 > vs < T 扩展数字 >

这两个函数之间的区别是什么?

static void gPrint(List<? extends Number> l) {
for (Number n : l) {
System.out.println(n);
}
}


static <T extends Number> void gPrintA(List<T> l) {
for (Number n : l) {
System.out.println(n);
}
}

我看到了相同的输出。

20621 次浏览

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).

When you use T you can perform all type of actions on List. But when you use you can not perform add.

T - as same as object reference with full access
? - give partial access

static void gPrint(List<? extends Number> l) {
l.add(1); //Will give error
for (Number n : l) {
System.out.println(n);
}


static <T extends Number> void gPrintA(List<T> l) {
l.add((T)1); //We can add
for (Number n : l) {
System.out.println(n);
}