Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Octal Value Assigning it to int

Tags:

java

In a quiz I found one question where i need to calculate out of below program

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

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

It gives compilation error on

 int b = 08;

As it an octal value, So I tried some 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

As 08 and 010 have same decimal number then why 08 gives compilation error.

like image 862
Amit Avatar asked Dec 09 '22 01:12

Amit


1 Answers

Because in octal notation, 010 != 08. Actually, 08 doesn't exist in octal number system. All you can use are numbers 0-7 (starting with a 0).

like image 89
TheLostMind Avatar answered Dec 20 '22 23:12

TheLostMind