I am using Java SE Development kit 8 update 25(64 bit). I have read the "Primitive Data Types" java tutorial from The Java™ Tutorials and according to this specification the long type can hold the maximum value of (2 to the power 64)-1 which is 18446744073709551615.
But the below code throws "integer number too large" error message.
public class SampleClass
{
public static void main (String[] args)
{
Long longMaxValue= 18446744073709551615L /* UInt64.MaxValue */;
}
}
Also the Long.Max_Value only returns 9223372036854775807 . Why? I need to store the value 18,446,744,073,709,551,615 as a constant in java. What should I do?
Any help will be appriciated.
The max positive value of long is (2 to the power of 63) - 1, since long in Java is signed, and therefore can hold negative integers. The left most bit is reserved for the sign, which leaves 63 bits for the value itself.
You can use BigInteger
if Long.MAX_VALUE
is not big enough for you.
Another option, if you are using Java 8 and wish to treat long
as unsigned is to use the unsigned methods of Long
:
For example :
long l = parseUnsignedLong("18446744073709551615");
If you print l
with System.out.println(l)
, you'll see a negative value, but if you print Long.toUnsignedString(l)
you'll see the unsigned value.