Why am I getting an "integer too large" error? I include an "L" at the end

The following code does not compile in Java:

java version "1.6.0_24" OpenJDK Runtime (IcedTea6 1.11.1) (suse-3.1-x86_64) OpenJDK 64-Bit Server VM (build 20.0-b12, mixed mode)

public class XOR
{
    public static void main(String[] args)
    {
        long one = 595082963178094600000L;
    }
}

      

This throws an error:

XOR.java:5: integer number too large: 595082963178094600000

      

But I put it right for how long! The following also throws an error:

public class XOR
{
    public static void main(String[] args)
    {
        long one = new Long( "595082963178094600000" );
    }
}

      

This throws:

java.lang.NumberFormatException: For input string: "595082963178094600000"

      

What am I doing wrong?

+3


source to share


2 answers


Well, maybe because it is too large

?

595082963178094600000  //your value
  9223372036854775807  //Long.MAX_VALUE

      



You will need BigInteger

either BigDecimal

:

new BigInteger("595082963178094600000")

      

+18


source


Values ​​for longs must be between -9,223,372,036,854,775,808 and 9,223,372,036,854,775,807 inclusive. You cannot assign a value greater than this to a variable that is long, even if you add L to it, it will overflow the value and cause a compile-time error.



+2


source







All Articles