Why does '08' generate error within the Eclipse?

0

I have a class with ' int = dia, mes e ano ' and in the other I'm calling them. But it caught my attention was that when I put the value '08' says

  

'The literal 08 of type int is out of range'.

But when I put 07, compile without problems.

I'll enter the codes:

    public class Data {
    int dia;
    int mes;
    int ano;
}
    public class DataTeste {
    public static void main(String[] args) {
        Data nascimento = new Data();
        **nascimento.dia = 08;**--este valor da erro, mas 07 p/baixo valida.
        nascimento.mes = 03;
        nascimento.ano = 1989;
    }
}
    
asked by anonymous 25.02.2018 / 14:21

2 answers

1

Any number prefixed with 0 is considered octal. Octal numbers can only use digits 0 through 7, just as the decimal can use 0-9 and the 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

That is, internally your "day" is having 3 numbers.

    
25.02.2018 / 14:48
1
The problem is that in most languages a literal integer starting with zero is interpreted as being an octal value, that is why until 07 it works because there is no octal representation of these numbers the right one would be you never start a number with 0.

    
25.02.2018 / 14:44