Why am I getting the error "integer number too large"? I'm including an "L" at the end

Go To StackoverFlow.com

3

The following code is not compiling in Java:

java version "1.6.0_24" OpenJDK Runtime Environment (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 the error:

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

But I've properly indicated it as a 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?

2012-04-05 18:12
by Don Rhummy
have you tried the second one without quotes - RyanS 2012-04-05 18:15
Are you sure that number isn't out of range of longs? If I recall the max long is ~9 quintillion which is smaller than your 595,082,963,178,094,600,00 - Chris 2012-04-05 18:17
This number looks so large! I hope you are counting money - dasblinkenlight 2012-04-05 18:20


17

Well, maybe because it is too large?

595082963178094600000  //your value
  9223372036854775807  //Long.MAX_VALUE

You will need either BigInteger or BigDecimal:

new BigInteger("595082963178094600000")
2012-04-05 18:15
by Tomasz Nurkiewicz
Thanks. I was thrown off because the error did not say the long was too large, but the integer was too large - Don Rhummy 2012-04-05 19:40


2

The values for a long must be be between -9,223,372,036,854,775,808 and 9,223,372,036,854,775,807 inclusive. You cannot assign a value larger than these to a variable that's a long, even if you append an L to it, it will overflow the value and cause an error at compile time.

2012-04-05 18:42
by SunKing2
Ads