Search code examples
javaliteralsoctal

Numeric literals in Java - octal?


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???


Solution

  • -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.