¿Por qué es
08 considered an out of range int but
07 and below are not?
preguntado el 28 de agosto de 11 a las 02:08
In Java and several other languages, an integer literal beginning with
0 is interpreted as an octal (base 8) quantity.
For single-digit numbers (other than
09, which are not allowed), the result is the same, so you might not notice that they are being interpreted as octal. However, if you write numbers with more than one significant digit you might be confused by the result.
010 == 8 024 == 20
Since octal literals are usually not what you want, you should always take care to never begin an integer literal with
0, unless of course you are actually trying to write zero by itself.
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.
Leading zero means the value is in octal. 8 is not an octal digit, no more than 2 is valid in binary or G is valid in hexadecimal.
In Java, if you are defining an int with a leading '0' denotes that you are defining a number in Octal.
int a = 08 is giving out of range error because there is no any number '8' in Octal. Octal provides 0-7 numbers only.
Si tu defines a = 07 then it's not giving out of range error because the numbers '0' and '7' are within the Octal's range.
In most of programming language like
C/C++, the number with leading zero are interpreted as número octal. As we know octal numbers are only represented within
7 digits only. Hence numbers like
054 are valid but the numbers like
08 are tends to invalid.