3

My friends and I have puzzled over this statement in Java after seeing it and the answer. How does this work?

System.out.printf("%d", 077);

equals 63?

4
  • 1
    This has nothing to do with printf. Commented Feb 4, 2015 at 21:16
  • oracle is here for you docs.oracle.com/javase/tutorial/java/data/numberformat.html Commented Feb 4, 2015 at 22:06
  • @David Conrad : Yes I know, I realized that after testing it in Java after receiving the answer about 077 as an Octal Number but I only asked that because it was a test questions my friends and I had and were completely confused on. Commented Feb 14, 2015 at 17:22
  • @crAlex : have already been there before I even asked the question and that does not explain anything about Octal Numbers even though I have asked about printf. Commented Feb 14, 2015 at 17:23

3 Answers 3

7

077 is an octal number which equals 7 x 81 + 7 x 80 which is 63 decimal. To display the octal value you could do

System.out.printf("%o", 077);
Sign up to request clarification or add additional context in comments.

Comments

2

When you define an literal integer number with a 0 prefix, the compiler will treat it as an integer base 8. (Octal).

Check at this post http://rodrigosasaki.com/2013/06/10/number-literals-in-java/

So, 77 value in Octal base is actually 63 in Decimal base.

Comments

0

077 = 7 * 8^0 + 7 * 8^1 = 63; 0123 = 3 * 8^0 + 2 * 8^1 + 1 * 8^2 = 3 + 16 + 64 = 83; First 0 means octal value.

0x77 - is hex val.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.