Java numeric literals - octal?

Here is some code in java on data types:

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

Output code above -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 day off different? What are the calculations performed for this code?

+4
source share
1 answer

-0777it is processed by the compiler as an octal number (base 8), the decimal value of which is -511 (- (64 * 7 + 8 * 7 + 7)). -777is a decimal number.

+8
source

All Articles