4

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

2
  • I would upvote this question, but the title is way too general and unspecific. Commented Jun 22, 2015 at 10:40
  • This question has nothing to do with data types. It is about numeric literals. Commented Jun 22, 2015 at 10:47

1 Answer 1

8

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

Sign up to request clarification or add additional context in comments.

3 Comments

what happens if I input “-077” via STDIN?
@RishavKundu "-077" is a String. There are numerous ways you can interpret/parse it.
@RishavKundu That would depend on how you read it from STDIN. For example - Scanner sc = new Scanner (System.in); System.out.println (sc.nextInt ()); will print -77 if you type in -077.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.