long - java unsigned short
Why doesn't Java support unsigned ints? (10)
Why doesn't Java include support for unsigned integers?
It seems to me to be an odd omission, given that they allow one to write code that is less likely to produce overflows on unexpectedly large input.
Furthermore, using unsigned integers can be a form of self-documentation, since they indicate that the value which the unsigned int was intended to hold is never supposed to be negative.
Lastly, in some cases, unsigned integers can be more efficient for certain operations, such as division.
What's the downside to including these?
This guy says because the C standard defines operations involving unsigned and signed ints to be treated as unsigned. This could cause negative signed integers to roll around into a large unsigned int, potentially causing bugs.
As soon as signed and unsigned ints are mixed in an expression things start to get messy and you probably will lose information. Restricting Java to signed ints only really clears things up. I’m glad I don’t have to worry about the whole signed/unsigned business, though I sometimes do miss the 8th bit in a byte.
I can think of one unfortunate side-effect. In java embedded databases, the number of ids you can have with a 32bit id field is 2^31, not 2^32 (~2billion, not ~4billion).
I know this post is too old; however for your interest, in Java 8 and later, you can use the
int data type to represent an unsigned 32-bit integer, which has a minimum value of 0 and a maximum value of 232−1. Use the
Integer class to use
int data type as an unsigned integer and static methods like
divideUnsigned() etc. have been added to the
Integer class to support the arithmetic operations for unsigned integers.
I think Java is fine as it is, adding unsigned would complicate it without much gain. Even with the simplified integer model, most Java programmers don't know how the basic numeric types behave - just read the book Java Puzzlers to see what misconceptions you might hold.
As for practical advice:
If your values are somewhat arbitrary size and don't fit into
long. If they don't fit into
Use the smaller types only for arrays when you need to save space.
If you need exactly 64/32/16/8 bits, use
byteand stop worrying about the sign bit, except for division, comparison, right shift, and casting.
See also this answer about "porting a random number generator from C to Java".
I've heard stories that they were to be included close to the orignal Java release. Oak was the precursor to Java, and in some spec documents there was mention of usigned values. Unfortunately these never made it into the Java language. As far as anyone has been able to figure out they just didn't get implemented, likely due to a time constraint.
Reading between the lines, I think the logic was something like this:
- generally, the Java designers wanted to simplify the repertoire of data types available
- for everyday purposes, they felt that the most common need was for signed data types
- for implementing certain algorithms, unsigned arithmetic is sometimes needed, but the kind of programmers that would be implementing such algorithms would also have the knowledge to "work round" doing unsigned arithmetic with signed data types
Mostly, I'd say it was a reasonable decision. Possibly, I would have:
- made byte unsigned, or at least have provided a signed/unsigned alternatives, possibly with different names, for this one data type (making it signed is good for consistency, but when do you ever need a signed byte?)
- done away with 'short' (when did you last use 16-bit signed arithmetic?)
Still, with a bit of kludging, operations on unsigned values up to 32 bits aren't tooo bad, and most people don't need unsigned 64-bit division or comparison.
The reason IMHO is because they are/were too lazy to implement/correct that mistake. Suggesting that C/C++ programmers does not understand unsigned, structure, union, bit flag... Is just preposterous.
Ether you were talking with a basic/bash/java programmer on the verge of beginning programming a la C, without any real knowledge this language or you are just talking out of your own mind. ;)
when you deal every day on format either from file or hardware you begin to question, what in the hell they were thinking.
A good example here would be trying to use an unsigned byte as a self rotating loop. For those of you who do not understand the last sentence, how on earth you call yourself a programmer.
This is from an interview with Gosling and others, about simplicity:
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.
With JDK8 it does have some support for them.
We may yet see full support of unsigned types in Java despite Gosling's concerns.