Skip to content
Advertisement

Why int j = 012 giving output 10?

In my actual project It happened accidentally here is my modified small program.

I can’t figure out why it is giving output 10?

public class Int
{
    public static void main(String args[])
    {
        int j=012;//accidentaly i put zero 
        System.out.println(j);// prints 10??
    }
}

After that, I put two zeros still giving output 10.

Then I change 012 to 0123 and now it is giving output 83?

Can anyone explain why?

Advertisement

Answer

Than I change 012 to 0123 and now it is giving output 83?

Because, it’s taken as octal base (8), since that numeral have 0 in leading. So, it’s corresponding decimal value is 10.

012 :

(2 * 8 ^ 0) + (1 * 8 ^ 1) = 10

0123 :

(3 * 8 ^ 0) + (2 * 8 ^ 1) + (1 * 8 ^ 2) = 83
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement