I was initially surprised that Java decides to specify that byte
is signed, with a range from -128..127
(inclusive). I'm under the impression that most 8-bit number representations are unsigned, with a range of 0..255
instead (e.g. IPv4 in dot-decimal notation).
So has James Gosling ever been asked to explain why he decided that byte
is signed? Has there been notable discussions/debates about this issue in the past between authoritative programming language designers and/or critics?
"I'm under the impression that most 8-bit number representations are unsigned..." Well, the ones called "byte" usually are, yeah. The ones called "char" tend not to be. Gosling wanted to keep everything signed, which is fair 'nuff, though I really wish he'd gone with a different name for it the 8-bit signed number.
Why cast byte to int? Java uses two's complement to represent signed numbers (positive and negative), the leftmost bit denotes the sign (0 is positive, 1 is negative), the rest of the bits represents the values from -128 (-2^7) to 127 (2^7-1) , the so-called 8-bit byte only have 7 bits to store the values.
It appears that simplicity was the main reason. From this interview:
Gosling: For me as a language designer, which I don't really count myself as these days, what "simple" really ended up meaning was could I expect J. Random Developer to hold the spec in his head. That definition says that, for instance, Java isn't -- and in fact a lot of these languages end up with a lot of corner cases, things that nobody really understands. Quiz any C developer about unsigned, and pretty soon you discover that almost no C developers actually understand what goes on with unsigned, what unsigned arithmetic is. Things like that made C complex. The language part of Java is, I think, pretty simple. The libraries you have to look up.
My initial assumption was that it's because Java doesn't have unsigned numeric types at all. Why should byte
be an exception? char
is a special case because it has to represent UTF-16 code units (thanks to Jon Skeet for the quote)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With