Dindi Rave Dindi Rave - 1 year ago 82
Java Question

What is the possible reason to get this array output?

int mark[] = {016,51,024,0150};
for(int e:mark)

The output for the above code was:


Can someone explain me why 016 became 14?

(and also why 024 and 0150 became that way) Why would having a zero in front make such a difference? Thanks in advance

Answer Source

A leading 0 on an integer literal indicates octal (base 8); see JLS§3.10.1. 016 octal is 14 decimal (base 10), since 016 octal has a 1 in the eights place and a 6 in the ones place. 8 + 6 = 14 decimal. Three of your four literals are in octal; 51 is in decimal. System.out.println(int) defaults to decimal, which is why you see what appear to be different values for the three that are octal but not for the one that isn't.

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