Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: Unsigned numbers

Is there a way in Java to use unsigned numbers like in (My)SQL?

For example: I want to use an 8-bit variable (byte) with a range like: 0 ... 256; instead of -128 ... 127.

like image 492
Martijn Courteaux Avatar asked Dec 03 '22 14:12

Martijn Courteaux


1 Answers

No, Java doesn't have any unsigned primitive types apart from char (which has values 0-65535, effectively). It's a pain (particularly for byte), but that's the way it is.

Usually you either stick with the same size, and overflow into negatives for the "high" numbers, or use the wider type (e.g. short for byte) and cope with the extra memory requirements.

like image 172
Jon Skeet Avatar answered Dec 23 '22 10:12

Jon Skeet