“Integer too large” for a small compile time constant

后端 未结 4 1450
深忆病人
深忆病人 2020-12-22 03:04

I have this:

switch (account){
    case 08120:
        // Savings deposit interest rate
        return null;                
    case 13100:
        // Recei         


        
相关标签:
4条回答
  • 2020-12-22 03:29

    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

    0 讨论(0)
  • 2020-12-22 03:42

    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.

    0 讨论(0)
  • 2020-12-22 03:43

    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.

    0 讨论(0)
  • 2020-12-22 03:46

    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.

    0 讨论(0)
提交回复
热议问题