why is not (123 == 0123) in java?

前端 未结 3 1494
礼貌的吻别
礼貌的吻别 2020-12-06 09:12

I am developing an application in Android using Eclipse. I wrote the following code and in tests the first and third \"if\" block is not reachable. Why?

3条回答
  •  难免孤独
    2020-12-06 10:10

    because 0123 in not decimal digit its octal (base 8) so this is equal to 83

    To convert a number k to decimal, use the formula that defines its base-8 representation:

    enter image description here

    0123 base-8 = 83 decimal
    
    0123 = (3*8^0) +(2*8^1)+(1*8^2)+(0*8^4)
         =3+16+64+0
         =83   
    

    An octal numeral consists of an ASCII digit 0 followed by one or more of the ASCII digits 0 through 7 and can represent a positive, zero, or negative integer.

    Note: Octal values are denoted in java by leading zero normal decimal number cannot have a leading zero

提交回复
热议问题