Dindi Rave - 5 months ago 25

Java Question

`int mark[] = {016,51,024,0150};`

for(int e:mark)

System.out.println(e);

The output for the above code was:

`14`

51

20

104

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

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.