Freestanding type parameter in Java Generics

Sandeep :

The type parameter section of Java generic types follows the generic class name. For example:

class name<T1, T2, ..., Tn> { /* ... */ }

It's quite intuitive once you get a hang of it. However I am completely stumped to see some freestanding type parameter sections in the default interface methods of Comparator<T>. For example:

default <U> Comparator<T> thenComparing(
        Function<? super T, ? extends U> keyExtractor,
        Comparator<? super U> keyComparator)

Clearly the method is returning some kind of comparator object, but how do you explain the freestanding <U> in English terms?

Andronicus :

It introduces the type parameter itself. If you were to write the method signature without it:

Comparator<T> thenComparing(
        Function<? super T, ? extends U> keyExtractor,
        Comparator<? super U> keyComparator)

that would mean, there should exist some concrete type named U visible to the method.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=305238&siteId=1