4

How does this snippet of code prints "-511" as the output on the console?

   class Test
   {
    public static void main(String[] args) {
    int i = -0777; 
    System.out.printf("%d",i);
    }
   }

Is it to do with the way Java stores Negative numbers?

2
  • 1
    That prints -63 for me :) Commented Sep 28, 2015 at 20:22
  • Sorry it was a typo error. For -077 it prints -63. I have updated the snipet. But doesnt matter. Commented Sep 28, 2015 at 20:26

3 Answers 3

12

Integer numbers prefixed with 0 are octal numbers. To use decimal numbers remove the 0 prefix:

int i = -777;
Sign up to request clarification or add additional context in comments.

Comments

4

Numbers starting with 0 are treated as being in octal by Java. -077 is equivalent to -63, which is what I get when I run your program.

Comments

2

When a number in java code starts with a 0, it interprets it as octal format

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.