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

- 104,088
- 27
- 192
- 230

- 515
- 1
- 4
- 5
-
see also http://stackoverflow.com/questions/35521278/printing-an-integer-in-java-that-have-zero-in-front-of-it/35521965#35521965 – bvdb Feb 20 '16 at 11:14
-
see also [Integer with leading zeroes](https://stackoverflow.com/q/565634/5221149) – Andreas Feb 13 '20 at 16:03
6 Answers
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 08
and 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.
For example:
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.

- 3,994
- 25
- 23
-
2Also note that the error message is depending on the base: `0b3` - binary - "binary numbers must contain at least one binary digit" `09` - octal - "integer number too large: 09" `0xG` - hexadecimal - "hexadecimal numbers must contain at least one hexadecimal digit" – Timmos Aug 06 '13 at 09:24
-
Jolly good to know. Someone might write a program with an array *myarray[010]* thinking that it's equivalent to *myarray[10]* and a plane crashes or whatever... – Kovács Imre Nov 29 '13 at 10:18
-
-
1`00` is octal for `0`. You have `0 == 00 == 0b0 == 0x0` for decimal, octal, binary, hexadecimal. As already mentioned by @Stuart Cook, they don`t differ as long as you only have the least common possible single digit. – Zabuzard Aug 16 '16 at 13:57
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.

- 79,954
- 26
- 128
- 166
-
I had to google a lot of bad base jokes on reddit to get it, but have finally learned that I'm in the 10th group, the ones who think that is a stupid joke. :) – CorayThan Oct 15 '16 at 06:43
From the Java specification:
An octal numeral consists of an ASCII digit 0 followed by one or more of the ASCII digits 0 through 7 and can represent a positive, zero, or negative integer.

- 7,958
- 12
- 47
- 62
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.
If you define a = 07 then it's not giving out of range error because the numbers '0' and '7' are within the Octal's range.

- 8,890
- 6
- 44
- 59
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 most of programming language like Java
and C/C++
, the number with leading zero are interpreted as octal number. As we know octal numbers are only represented within 0
to 7
digits only. Hence numbers like 05
,03
,054
are valid but the numbers like 078
,0348
,09
,08
are tends to invalid.