Why int j = 012 giving output 10?

前端 未结 4 1706
抹茶落季
抹茶落季 2020-11-30 11:15

In my actual project It happened accidentally here is my modified small program.

I can\'t figure out why it is giving output 10?

pub         


        
4条回答
  •  旧巷少年郎
    2020-11-30 12:18

    If a number is leading with 0, the number is interpreted as an octal number, not decimal. Octal values are base 8, decimal base 10.

    System.out.println(012):
    (2 * 8 ^ 0) + (1 * 8 ^ 1) = 10
    12 = 2*8^0 + 1*8^1 ---> 10
    
    
    System.out.println(0123)
    (3 * 8 ^ 0) + (2 * 8 ^ 1) + (1 * 8 ^ 2) = 83
    123 = 3*8^0 + 2*8^1 + 1*8^2 ---> 83
    

提交回复
热议问题