r/java • • 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?

62 Upvotes

94 comments sorted by

View all comments

Show parent comment

1

u/davidalayachew Jan 20 '26

I think everything else about your comment is well-founded and makes good sense, except this snippet.

An intermediate NormalNumber class

That's just not what java is. Java doesn't have a deeply typed hierarchy of e.g. List, MutableList, and ImmutableList either. Perhaps java should have that, but a haphazard whack-a-mole game where parts of the existing API grow like trees out of seeds into a type hierarchy that separate concerns and bend over backwards to avoid LISKOV violations is a bad idea; best to do it in one go, and the time was never there.

The entire reason why the JDK doesn't support things like mutation in the Collections API is because it would lead to a combinatorial explosion.

When you add mutation to Collections, you have to multiply it by at least 4, if not 8. You need MutableCollection, MutableSet, MutableList, and MutableMap. And then it would be painful if there wasn't also an Immutable variant of the above. Thus, you are stuck with 4-8 times the maintenance fee. This is what I mean by 4 or 8.

Number doesn't have that problem. It is 1 single point of extension. Therefore, any functionality you want to implement is just that -- 1 single point of extension.

I still assert that some RealNumber abstract class is the real solution here.

1

u/rzwitserloot Jan 20 '26

When you add mutation to Collections, you have to multiply it by at least 4, if not 8

Okay. Why stop at NormalNumber then? If we're adding that ('numbers that are comparable'), what about 'numbers that have a commutative + operation' for example?

And 'x4' is not a fair contrast. Number is 1 class, Set, Collection, List, and Map are 4. It's not about 'omg 8 new classes!' it's simply about 'double the classes'. Going from Number to Number + NormalNumber is going from 1 to 2 - also a doubling.

I still assert that some RealNumber abstract class is the real solution here.

Then you've really missed multiple points, as this is a terrible idea.

1

u/davidalayachew Jan 21 '26

Okay. Why stop at NormalNumber then? If we're adding that ('numbers that are comparable'), what about 'numbers that have a commutative + operation' for example?

And 'x4' is not a fair contrast. Number is 1 class, Set, Collection, List, and Map are 4. It's not about 'omg 8 new classes!' it's simply about 'double the classes'. Going from Number to Number + NormalNumber is going from 1 to 2 - also a doubling.

It's precisely because it is not a fair contrast that I say this might be worth doing.

If every new feature I want requires me to create 4 new types, that's expensive. If every new feature I want only requires me to create 1 new type, then that might not be too bad. Hence my point.

The point is that these 2 cases have a different slope of increase, and therefore, I don't think NormalNumber is a problem.

Then you've really missed multiple points, as this is a terrible idea.

Feel free to let me know where I went wrong.