-1

Can someone explain what this does? This is a generics question but since I'm learning in Java I tagged it as Java.

public static <T extends Comparable<? super T>> void quickSort(T[] list) {...}

What I don't understand is this part: <T extends Comparable<? super T>>

I have tried looking at this but it doesn't seem to explain it in a clear way.

My guess is: the genetic type T extends the Comparable interface which itself requires a generic type to be a superclass of a generic type. But I don't really understand this.

  • 3
    You could start [here](http://docs.oracle.com/javase/tutorial/java/generics/index.html). – Christian Tapia Jun 07 '14 at 08:35
  • Thanks. Well I do understand the ? and somewhat extends T> and super T> though I'm only going through this concept briefly as part of a course curriculum so I think it's going to take too much time, so if there are some really helpful examples as a starter it would be great. – user3717271 Jun 07 '14 at 08:43

1 Answers1

1

Example:

public class X implements Comparable<X> {
}

The class X implements Comparable which satisfies extends Comparable and the generic parameter of Comparable is X because X is super X (java generics super keyword)

Another example:

public class Y {}

public class X extends Y implements Comparable<Y> {
}

Another example:

public class X implements Comparable<Object> {
}

But this won't work:

public class A {}

public class X implements Comparable<A> {
}

As the class X is only comparable to objects that are not in it's inheritance hierarchy.

The sorting algorithm is just going to call x.compareTo(y) and it needs to know at compile time that that is valid.

Community
  • 1
  • 1
weston
  • 54,145
  • 21
  • 145
  • 203