Jekin Kalariya Jekin Kalariya - 1 year ago 70
Java Question

Compiler behavior different on Integer.Why below code gives compiler error in one case?

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


Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download