2

For my Java class, I need to make a function body given this function header:

public static <T> void sort(T[] a, Comparator<? super T> c)

What does the "< T> " mean right after Static and what is the Comparator with the question marks? My knowledge of Comparators is that it's an interface which you can implement and it has two methods .compare and .equal.

Thanks.

uraimo
  • 19,081
  • 8
  • 48
  • 55
Mike
  • 37
  • 6
  • 1
    is Java generics. See http://en.wikipedia.org/wiki/Generics_in_Java The question mark in the second case is a wildcard that basically says: give me any class that extends class "T". – Shanqing Cai Mar 14 '15 at 20:10

1 Answers1

2

Comparator is a parameterized type and T is a type parameter, you should check out some documentation on Java 5 Generics.

Post-java5, the compare method in the Comparator interface now have this signature:

int compare(T o1,T o2)

Simplifying a lot, T is a placeholder for the generic type that a particular instance of Comparator is able to manage, pre-java5 it would have been an object.

See also this similar question.

uraimo
  • 19,081
  • 8
  • 48
  • 55