Clarification about “int” number that begins with 0

后端 未结 4 1195
难免孤独
难免孤独 2021-01-20 05:28
public class Test {

    public static void main(String[] args) {
        int i = 012;
        System.out.println(i);
    }
}

Why the output is : <

4条回答
  •  悲&欢浪女
    2021-01-20 05:58

    See the JLS:

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

    It's a good practice to write:

    int i = 0_12;
    

    It might be clearer now, i in decimal is 2*80 + 1*81 = 10.

提交回复
热议问题