Why is 08 not a valid integer literal in Java?

后端 未结 6 1058
清歌不尽
清歌不尽 2020-11-22 06:30

Why is 08 considered an out of range int but 07 and below are not?

6条回答
  •  忘掉有多难
    2020-11-22 06:50

    Any number prefixed with a 0 is considered octal. Octal numbers can only use digits 0-7, just like decimal can use 0-9, and binary can use 0-1.

    // octal to decimal
    01  // 1
    02  // 2
    07  // 7
    010 // 8
    020 // 16
    
    // octal to binary (excluding most significant bit)
    01  // 1 
    02  // 10
    07  // 111
    010 // 1000 
    020 // 10000
    

    There are 10 types of people, those who understand ternary, those who don't, and those who think this is a stupid joke.

提交回复
热议问题