javagenericsbounded-wildcardbounded-types

Difference between Bounded Type parameter (T extends) and Upper Bound Wildcard (? extends)


I know that there was a similar question already posted, although I think mine is somewhat different...

Suppose you have two methods:

// Bounded type parameter
private static <T extends Number> void processList(List<T> someList) {

}

// Upper bound wildcard
private static void processList2(List<? extends Number> someList) {
    // ...
}

As far as I know, both methods accepts arguments, that are List of type Number or List of subtype of Number.

But what's the difference between the two methods after all?


Solution

  • There are several differences between the two syntaxes during compile time :

    Note that since generics are compile time sugar, these differences at a broader level are only limited to the compilation.