Java Why use restricted wildcards in this example?

I am doing a practical exam for one of my classes, I ran into a problem that asked me to implement a static universal method. I was right for the method body, but my assumption for the actual method signature was different.

I guessed:

public static <T> boolean isSorted(T[] array, Comparator<T> cmp){ ...

The answer to the practice exam, however, used a limited wildcard similar to this:

public static <T> boolean isSorted(T[] a, Comparator<? super T> cmp)

I read javadoc again, and although I know what that means (it superis restrictive in terms of inclusion in the class hierarchy for the type you specify T), I don’t think I fully understand why you would like to use a limited template in this form.

Thanks in advance.

+4
1

Java , , - , Integer, , Comparator, Integer, Comparator , , Number. , Comparator .

+6

Source: https://habr.com/ru/post/1543363/


All Articles