0

Behavior of below code like this.First declaration gives compiler error while second works fine, even we print result of second line its give output of 1270 not 02366. so is it any specific truncation or shifting in integer in such cases?

public static void main(String[] args) {

        int i =01339;//compiler error out of int range

        int j= 02366;//works fine

       System.out.println(j); //value 1270

}
Jekin Kalariya
  • 3,475
  • 2
  • 20
  • 32

2 Answers2

2

Literals starting in 0 are octal (base 8) literals, which may only contain the digits 0 to 7. Therefore 01339 is not valid.

02366 is 2*8*8*8 + 3*8*8 + 6*8 + 6 = 1270

Eran
  • 387,369
  • 54
  • 702
  • 768
0

Because in java when you define number starting with 0 it thinks as base 8 number and hence it converts into base 10 and return 1270 as output.

Only possible literals for base 8 are 0 to 7 and here you have entered 9 which is outside the allowed value and hence shows the error

Naruto
  • 4,221
  • 1
  • 21
  • 32