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

Concluding

The reason we got here is: History

The reason we stay here is:

  • The 'bang' is much lower than you think.
  • The 'buck' is much higher than you think.

Well, until we get typeclasses. I assume that that is what your comment is also implying? Because once that is true, your point about Peano Arithmetic becomes not too difficult to implement. Maybe a little tedious, but absolutely doable.

1

u/rzwitserloot Jan 20 '26

Maybe. Let's not get ahead of ourselves. My point is: Without typeclasses the idea is pointless, given that you can't add peano math without them.

Note that the reverse isn't stated. I never said that you can add peano math with them. typeclasses aren't out yet, nor are they in an advanced of state of completion. I don't think 'finally add peano math to java!' is a stated goal of the typeclass proposal even. It's that unimportant.

1

u/davidalayachew Jan 21 '26

Maybe. Let's not get ahead of ourselves. My point is: Without typeclasses the idea is pointless, given that you can't add peano math without them.

Note that the reverse isn't stated. I never said that you can add peano math with them. typeclasses aren't out yet, nor are they in an advanced of state of completion. I don't think 'finally add peano math to java!' is a stated goal of the typeclass proposal even. It's that unimportant.

Well, we got our first formal introduction to Java's considered approach to typeclasses a few months ago, so I'd say it's premature to say that its absence in the proposal indicates its lack of importance.

I'll raise it once a JEP goes live. Then we'll see.

Either way, thanks for the insight on Typeclass interaction with Peano Arithmetic. That's something I didn't know about before.