P45 Imminent
P45 Imminent

Reputation: 8591

"Integer too large" for a small compile time constant

I have this:

switch (account){
    case 08120:
        // Savings deposit interest rate
        return null;                
    case 13100:
        // Receivables contractual interest
        return null;                
    case 16550:
       // Default management process accounts payable
       return null;                                
}

But the compiler complains tha the integer 08120 is too large!

What on earth is going on?

Upvotes: 1

Views: 158

Answers (4)

Minudika
Minudika

Reputation: 851

It's not a java bug.

You cannot use the format of 0XXXXX for decimal numbers. Decimal numbers should be started with a non-zero digit.

Use 8120 instead of 08120

Upvotes: 2

Madhav Datt
Madhav Datt

Reputation: 1083

This is a little misleading/cryptic error message. In your code:

case 08120:
    // Savings deposit interest rate
    return null; 

You have used 08120, which is being interpreted as an octal literal instead of a decimal one. Since your next digit is 8 (not between 0 - 7), it is an invalid/out-of-range octal literal and hence the compiler error.

Upvotes: 1

dan14941
dan14941

Reputation: 332

Numbers starting with a 0 are interpreted as an octal number also 8 would not fit in there since the valid octal digits can be 0 through to 7. This is not a bug, try starting your number without the 0.

Upvotes: 1

Bathsheba
Bathsheba

Reputation: 234785

First things first: it's unlikely you've stumbled across a Java bug. Blame your code first.

08120 is an octal literal in Java since it starts with a leading zero.

And 8 is not a valid octal digit (only 0 to 7 are).

Therefore you get a compilation error, albeit a little misleading.

Upvotes: 15

Related Questions