Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Numeric literals in Java - octal? [duplicate]

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

like image 265
Sowmya Avatar asked Feb 10 '23 18:02

Sowmya


1 Answers

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

like image 109
Eran Avatar answered Feb 12 '23 10:02

Eran