In the Oracle "Primitive data types" page, it mentions that Java 8 adds support for unsigned ints and longs:
int: By default, theintdata 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 theintdata type to represent an unsigned 32-bit integer, which has a minimum value of 0 and a maximum value of 232−1. Use theIntegerclass to useintdata type as an unsigned integer. See the section The Number Classes for more information. Static methods likecompareUnsigned,divideUnsignedetc have been added to theIntegerclass to support the arithmetic operations for unsigned integers.
long: Thelongdata type is a 64-bit two's complement integer. The signedlonghas a minimum value of −263 and a maximum value of 263−1. In Java SE 8 and later, you can use thelongdata type to represent an unsigned 64-bitlong, 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. TheLongclass also contains methods likecompareUnsigned,divideUnsignedetc to support arithmetic operations for unsignedlong.
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?