I create a variable of type int and I give it a value of, for example, "06" or some other value with a "0" in front, and this should show an error because if it is an integer it cannot have a zero to the left, but everything works fine. On the other hand, if we give it the value: "08" or "09", you get an error indicating that it is out of range.
The error should mark it with the entire sequence from "01" to "09", why only with "08" and "09"?
public static void main( String[] args ) {
int test = 08;
}
The literal 08 of type int is out of range
08
, contrary to what you think, is a literal number in octal format . Java interprets as numbers in that format all those that start with a0
.In this format, the individual digits go from
0
to7
.Hence the error:
08
is not a valid octal number , the same as09
. If they are correct, for example,05' o
03`.Googling this: literal int java leading zeros (since the question sounded interesting to me), I found this answer in English .
There it clarifies that if you put a leading 0, the value is taken as an octal. And octals are numbers from 0 to 7.
Therefore, your 6 is an octal 6, not a 6 in base ten.
Jon , in a later reply , provides us with a link to the java documentation .
There he clarifies the answer a little more, saying that a single 0, or the numbers from 1 to 9 are taken as decimals, and any number that begins with 0 is taken as octal.
As a curiosity, it clarifies that a 00 is a 0 in octal, as if it were really very different from a 0 in decimal.