javagenerics

Java Generics WildCard: <? extends Number> vs <T extends Number>


What is the difference between these 2 functions?

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);
    }
}

I see the same output.


Solution

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