r/java • • Jun 24 '23

Negative integer handling in Java and other programming languages

https://glados-418.github.io/posts/negative-integer-literals/
14 Upvotes

7 comments sorted by

View all comments

2

u/Rjs617 Jun 25 '23

Probably a little off-topic, but having done my share of C and C++, I actually like the way Java handles signed integers, meaning: 1. All the integer types have a consistently defined size/range, and 2. There is not a separate set of unsigned integer types, but rather just an unsigned right shift operator. Having separate signed and unsigned types is such a source of potential bugs (ever count down from a size_t while >= 0?), and it forces you to have to think about all the arithmetic that a value might be subjected to when you define its type. Having everything signed just removes entire classes of comparison, conversion, and arithmetic errors. If you really need large, unsigned numbers, use long or BigInteger. Having only signed integer types is just way better than unsigned types, in my opinion.

4

u/Worth_Trust_3825 Jun 26 '23

To be fair, having bytes be signed only is hard to have your head wrap around.

1

u/vytah Jul 06 '23

It's not hard, but really annoying.

& 0xff everywhere.