Numeric literals in Java - octal?

279 views Asked by At

Here is some code in java on datatypes:

class Test
{
    public static void main(String args[])
    {
        int i = -0777;
        System.out.println(i);
    }
}

The output of the above code is -511

If the code is changed to :

class Test
{
    public static void main(String args[])
    {
        int i = -777;
        System.out.println(i);
    }
}

The output is -777.

Why is the output differing??? What are the calculations done behind this code???

1

There are 1 answers

3
Eran On BEST ANSWER

-0777 is treated by the compiler as an octal number (base 8) whose decimal value is -511 (-(64*7+8*7+7)). -777 is a decimal number.