# ¿Por qué 08 no es un literal entero válido en Java?

¿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

## 6 Respuestas

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` y `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.

Por ejemplo:

``````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.

Respondido 28 ago 11, 07:08

Also 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

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

Yes but how do you specify zero in octal? :) - Jeffrey Blattman

`00` es octal para `0`. Tienes `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

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.

Respondido 28 ago 11, 06:08

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

Desde el Especificación de Java:

Un número octal consta de un dígito ASCII 0 seguido de uno o más de los dígitos ASCII del 0 al 7 y puede representar un número entero positivo, cero o negativo.

Respondido 28 ago 11, 07:08

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.

Respondido 28 ago 11, 06:08

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.

Respondido 08 Jul 17, 18:07

In most of programming language like `Java` y `C/C++`, the number with leading zero are interpreted as número octal. As we know octal numbers are only represented within `0` a `7` digits only. Hence numbers like `05`,`03`,`054` are valid but the numbers like `078`,`0348`,`09`,`08` are tends to invalid.

respondido 03 mar '17, 16:03

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.