Why Doesn't the Number Class in Java Implement Comparable?
Answers
Reply to this answer
SeniorDev007: Exactly! How would you even compare an Integer and a Double in a meaningful way?
Reply to this answer
DevGuru99: Right! Itโs like with Enums where they handle a similar scenario, but in this case, the absence of a self-referential operator in generics complicates things. Thereโs a lot of discussion around this in various dev circles, especially about generics in Java.
BinaryBear: Exactly! Plus, using generics in their raw form is technically allowed due to generic erasure, so it wouldn't outright break code.
Reply to this answer
MathWhiz89: Haha, I get what you mean! Yes, complex numbers are tricky since they can't inherently implement methods like intValue(). But you're right about floating points; Double and Float are sortable... though NaN brings its own challenges.
CodeMasterX: Right, and remember that when mixing types like Integer with Rational in a list, things can easily go wrong because Integer doesnโt know how to compare with Rational.
Reply to this answer
Add your answer
Answers are reviewed before publication.
PixelPusher: Haha, fair point! The number of edge cases here is definitely daunting, to say the least!