Discussion of why java.lang.Number does not implement Comparable



So, I have read the SDN entry of why Number does not implement
Comparable
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4414323

And while I agree with their thinking I disagree with their approach.

Not all Numbers are 'Comparable' such as NaN (which is one of their
motivational cases). However, when you look up the behavior of
compareTo() in Float -- no such special condition exists for the case
of NaN

While some numbers are not comparable, I argue that the numbers used
in Java (with the exception of NaN) are comparable. So, Double d =
10.12 and Integer x = 11 ARE comparable. X is clearly larger.

For me, it seems that a better approach would be to have Number
implement Comparable in the class signature but not actually DO the
implementation. That way, in the event of NaN and such, implementing
methods can simply throw exceptions (which seems far more appropriate
than what Float.compareTo(Float.NaN) produces now -- which is

int result = new Float(10).compareTo(Float.NaN);
System.out.println(result);

-1

Only special number cases are not comparable.

Thoughts, anyone? I fully realize I may bring out the worst in people
with this question

Christian Bongiorno
http://christian.bongiorno.org

.



Relevant Pages

  • Re: Discussion of why java.lang.Number does not implement Comparable
    ... Not all Numbers are 'Comparable' such as NaN (which is one of their ... compareTo() in Float -- no such special condition exists for the case ... public class Complex extends Number { ... private final double real; ...
    (comp.lang.java.programmer)
  • Re: Discussion of why java.lang.Number does not implement Comparable
    ... And while I agree with their thinking I disagree with their approach. ... Not all Numbers are 'Comparable' such as NaN (which is one of their ... compareTo() in Float -- no such special condition exists for the case ... subclasses -- say you have your own arbitrary-precision decimal class ...
    (comp.lang.java.programmer)
  • Re: NPE in PriorityQueue.poll()
    ... going wrong in compareTo(). ... other Foo and performs some math and logic. ... from an exception trace and one paraphrased line of code. ... What happens when you wrap your PriorityQueue ...
    (comp.lang.java.programmer)
  • Re: Java Arrays.sort throws exception
    ... My money is on some kind of bug in compareTo too. ... Could it be that the comparator must be consistent with equals for the sort to work? ... Contract which is: ... in particular x.compareTomust throw an exception if and only if y.compareTothrows an exception ...
    (comp.lang.java.programmer)