regarding leading zero in integer value

后端 未结 3 1730
野性不改
野性不改 2020-12-04 02:33

I have below code

int a = 01111;
System.out.println(\"output1 = \" + a);
System.out.println(\"output2 = \" + Integer.toOctalString(1111));

3条回答
  •  悲哀的现实
    2020-12-04 03:28

    If a number literal starts with 0 it denotes the octal base. Similarly 0x denotes hexadecimal base and 0b the binary number.

    So your

    int a=01111;
    

    .. is actually

    8^3 + 8^2 + 8^1 + 8^0 = 
    512 + 64 + 8 + 1 = 585
    

    The Integer.toOctalString(1111)) is actually a reverse function, i.e. the result is the octal number which is 1111 in decimal, which 2127 really is

    2127(oct) = 2 × 8^3 + 1 × 8^2 + 2 × 8^1 + 7 × 8^0 = 1111(dec)
    

提交回复
热议问题