Octal value Assigning an int value to it

In the quiz I found one question where I need to calculate the program below

 public static void main(String[] args) {
    short x = 0;
    int b = 08; 
    x +=b;

    System.out.println("" + b + x );
}

It gives a compilation error on

 int b = 08;

Like this is an octal value, so I tried several different values

 int b = 07 // working fine (decimal of same is 7)
 int b = 08 // (Decimal value 8) // Compilation error
 int b = 09 // (Decimal value 9) // Compilation error
 int b = 010 // (Decimal value 8) // No Compilation error

Since 08 and 010 have the same decimal number, why does 08 give a compilation error.

+4
source share
5 answers

Because in octal notation 010! = 08. Actually, 08does not exist in the octal system. All you can use are numbers 0-7(starting at 0).

+5
source

All integer literals starting with 0are considered octal.

, , 0 7 .

08 09, 010.

+4

, 0,1,2,3,4,5,6 7.

0, . 8 , .

, 8 010, 9 - 011 .

+3

, 8 . 8 : 0, 1, 2, 3, 4, 5, 6 7.

+2

JLS jls-OctalNumeral

ASCII 0, ASCII 0 7, , , .

OctalDigit:
( )
0 1 2 3 4 5 6 7

08 09

0

Source: https://habr.com/ru/post/1607963/


All Articles