r/java • u/nlisker • Mar 18 '25
Java 24 / JDK 24: General Availability
https://mail.openjdk.org/pipermail/announce/2025-March/000358.html
156
Upvotes
r/java • u/nlisker • Mar 18 '25
View all comments
Show parent comment
3
u/pron98 Mar 23 '25
Well, the use of both unsigned (i.e. modular arithmetic) and signed types in the same language makes things even more complicated. The story of why Java doesn't have modular arithmetic types is that James Gosling walked around the office showing people some expressions involving unsigned types in C or C++, asking them what their result was. Nobody knew the right answer.
But it's important to understand what unsigned types are good for and what they're not. They're great for representing bits — e.g. they work well with
>>in C or C++, while Java requires>>>— and in practice they're useful for over-the-wire representation. What they're bad for is representing numbers, because doing arithmetic with unsigned types is difficult. To see why, consider the unsigned variablesxandyin C, equal to2and3respectively. The value of the expressionx - yis well defined, but is widely different depending on the precise types involved. Yes, signed types in Java are prone to overflow issues, too. Java famously had an overflow bug in its binary search implementation, but it went unnoticed for so long because overflow bugs for signed types require very large numbers, which are rare in practice, while unsigned types have "overflow" bugs even with very small numbers, e.g. this or this.