1

Why do you think the definition of Comparable<T> lacks an upper bound on T?

That is, why is it not defined as:

Comparable<T extends Comparable<?>>

or

Comparable<T extends Comparable<? super T>>

Wouldn't the latter two proposals be closer to the intended correct use of the interface?

The interface documentation starts with the following sentence, suggesting that it's intended to be used as T implements Comparable<T>:

This interface imposes a total ordering on the objects of each class that implements it.

Is it just for backward compatibility with pre-generic code?

closed as primarily opinion-based by gnat, Bart van Ingen Schenau, BobDalgleish, Robert Harvey Apr 26 at 14:37

Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise. If this question can be reworded to fit the rules in the help center, please edit the question.

  • I don't know how you can say this is opinion based. It has the purely logical answer below – Ewan Apr 26 at 12:04
  • An answer that's being debated in the comments on its form, not its merit. And it's unclear from the OP why the proposed definitions would be any better. – Robert Harvey Apr 26 at 14:36
  • 1
    T doesn't need qualification for Comparable<T> to work. Your suggestions are limiting what T can be for no obvious reason. T and Comparable<T> are different types. The latter offers a way to compare the former. Comparable<T> places no requirement on T to be anything: T and Comparable<T> do not need to be in a (sub) class relationship – Erik Eidt Apr 26 at 18:50
  • "The interface documentation starts with the following sentence, suggesting that it's intended to be used as T implements Comparable<T>" Yes, but there is no way to declare it in Java to guarantee that an implementing class T will implement Comparable<T> In the way you suggested declaring it, interface Comparable<T extends Comparable<? super T>>, you can still declare class Foo implements Comparable<Foo> and then class Bar implements Comparable<Foo>. That would be completely legal according to your declaration. – user102008 Sep 2 at 21:43
2

Because you can have a type that is comparable to another type.

E.g.

class StringLike implements Comparable<String>
{
    int compareTo(String s) { /* something here */ }
}
  • 1
    You can do that, but you are abusing the interface, as proved by the fact that you can't use StringLike with any of the classes/methods that interact with Comparable, like TreeSet or Arrays.sort. – Marco Apr 26 at 12:38
  • You certainly can't use it with anything wanting a String, but you can use it with anything wanting a Comparable<String> – Caleth Apr 26 at 12:41
  • 1
    Is there anything in the JDK that works with Comparable<String>? AFAIK everything needs a T that implements Comparable<? super T>. – Marco Apr 26 at 12:51

Not the answer you're looking for? Browse other questions tagged or ask your own question.