In Java, the Integer class implements Comparable<Integer>
, Double implements Comparable<Double>
, and so on. This means you can write code like
new Integer(0).compareTo(new Integer(1))
However, you can not compare different types:
new Integer(0).compareTo(new Double(1)) // illegal!
Why don't the various Number subclasses implement Comparable
to each other?
Note that you can compare an integer and a double through 0 < 1.1
using primitives.