How does a leading zero change a numeric literal in Java?

后端 未结 3 1710
名媛妹妹
名媛妹妹 2020-11-29 13:40

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);
3条回答
  •  死守一世寂寞
    2020-11-29 14:17

    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.

提交回复
热议问题