Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use the unsigned Integer in Java 8 and Java 9?

In the Oracle "Primitive data types" page, it mentions that Java 8 adds support for unsigned ints and longs:

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. Use the Integer class to use int data type as an unsigned integer. See the section The Number Classes for more information. Static methods like compareUnsigned, divideUnsigned etc have been added to the Integer class to support the arithmetic operations for unsigned integers.

long: The long data type is a 64-bit two's complement integer. The signed long has a minimum value of −263 and a maximum value of 263−1. In Java SE 8 and later, you can use the long data type to represent an unsigned 64-bit long, which has a minimum value of 0 and a maximum value of 264−1. Use this data type when you need a range of values wider than those provided by int. The Long class also contains methods like compareUnsigned, divideUnsigned etc to support arithmetic operations for unsigned long.

However, I find no way to declare an unsigned long or integer. The following code, for example, gives a compiler error message of "the literal is out of range" (I'm using Java 8, of course), when it should be in range (the value assigned is precisely 264−1):

public class Foo {     static long values = 18446744073709551615L;          public static void main(String[] args){         System.out.println(values);     }   } 

So, is there any way to declare an unsigned int or long?

like image 417
Pabce Avatar asked Aug 28 '14 18:08

Pabce


People also ask

How can we use unsigned integer in Java?

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. Use the Integer class to use int data type as an unsigned integer.

Does Java support unsigned integers?

Java has been criticized for not supporting unsigned integers. Instead, its byte, short, int, and long types describe signed integers whose values are stored in two's complement form.

How do you use unsigned integers?

Unsigned integers are used when we know that the value that we are storing will always be non-negative (zero or positive). Note: it is almost always the case that you could use a regular integer variable in place of an unsigned integer.

What is unsigned value in Java?

An unsigned integer can hold a larger positive value, and no negative value like (0 to 255) . Unlike C++ there is no unsigned integer in Java.


2 Answers

Well, even in Java 8, long and int are still signed, only some methods treat them as if they were unsigned. If you want to write unsigned long literal like that, you can do

static long values = Long.parseUnsignedLong("18446744073709551615");  public static void main(String[] args) {     System.out.println(values); // -1     System.out.println(Long.toUnsignedString(values)); // 18446744073709551615 } 
like image 106
kajacx Avatar answered Sep 30 '22 03:09

kajacx


Per the documentation you posted, and this blog post - there's no difference when declaring the primitive between an unsigned int/long and a signed one. The "new support" is the addition of the static methods in the Integer and Long classes, e.g. Integer.divideUnsigned. If you're not using those methods, your "unsigned" long above 2^63-1 is just a plain old long with a negative value.

From a quick skim, it doesn't look like there's a way to declare integer constants in the range outside of +/- 2^31-1, or +/- 2^63-1 for longs. You would have to manually compute the negative value corresponding to your out-of-range positive value.

like image 41
Sbodd Avatar answered Sep 30 '22 02:09

Sbodd