r/java • u/davidalayachew • Jan 19 '26
Why doesn't java.lang.Number implement Comparable?
I found that out today when trying to make my own list implementation, with a type variable of <T extends Number>, and then that failing when passing to Collections.sort(list).
I would think it would be purely beneficial to do so. Not only does it prevent bugs, but it would also allow us to make more safe guarantees.
I guess a better question would be -- are there numbers that are NOT comparable? Not even java.lang.Comparable, but just comparable in general.
And even if there is some super weird set of number types that have a good reason to not extend j.l.Number, why not create some sub-class of Number that could be called NormalNumber or something, that does provide this guarantee?
View all comments
Show parent comment
1
u/davidalayachew Jan 20 '26
Well hold on, I already addressed this in my post -- I said that, on the off chance that it does not make sense to modify Number itself, then I am asking to create a new layer in the hierarchy for "real numbers". Name it whatever, but the goal is to add useful operations that can be expected of all "real numbers", one of which is the ability to compare to itself.
So, no -- my goal is not to modify Number, now that it has been made clear that there are in fact, many weird numbers that extend Number. Complex numbers, for example.
The point is that not having that middle abstract class of
RealNumbermakes it harder to do the work that I feel should be much easier.