As per Java API spec, the signature of Collections.reverseOrder is
public static <T> Comparator<T> reverseOrder()
And the example given in the method description says it needs to be used as
Arrays.sort(a, Collections.reverseOrder());
When we call the method, nowhere do we specify what type to use (what T resolves to).
How does the compiler resolve T in this case? Can the return type (T) be resolved based on the type of the object it is being assigned to?
Btw, I’m aware of the overloaded reverseOrder(Comparator<T> c) method.
Arrays.sort() knows what kind of Comparator it needs, since
Tis specified by the first argument (a):EDIT:
@Louis Wasserman correctly points out that we only need a
Comparator<? super T>, not aComparator<T>. SinceObjectis a superclass of anyT, thenComparator<Object>(the default if no generic parameters are given) is sufficient.