Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't Java support unsigned ints?

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?

like image 617
dsimcha Avatar asked Jan 10 '09 01:01

dsimcha


People also ask

Why there is no unsigned int in Java?

They're also a form of self-documentation for values that are never negative; for example, a counter or an array's length. Java's lack of support for unsigned integers often leads to a tedious translation process when translating to Java from C language–based source code that uses unsigned integers.

Can we use unsigned int in Java?

Java does not have a datatype for unsigned integers. You can define a long instead of an int if you need to store large values. You can also use a signed integer as if it were unsigned.

Is int in Java signed or unsigned?

int: By default, the int data type is a 32-bit signed two's complement integer, which has a minimum value of -231 and a maximum value of 231-1. In Java SE 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.

Is it good practice for unsigned int?

Unsigned is good programming practice to indicate the intention, to yourself and others, of use of the data element - just in the same way all types are used. For example, a normal array index variable should never be negative and so declaring the variable unsigned should be best practice.


1 Answers

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.

like image 192
Uri Avatar answered Oct 23 '22 08:10

Uri