Because '08' generates error inside the Eclipe?

Asked

Viewed 34 times

0

I have a class with 'int = dia, mes e ano' and on the other I’m calling them. Only that 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 smoothly.

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;
    }
}
  • First, why create a class for date if java has so many classes for that?

  • It’s just that I’m following an example from a video lesson...

2 answers

2

The problem is that in most languages a literal integer started by zero is interpreted to be an octal value, which is why until 07 works because there is no octal representation of these numbers the right one would be you would never start a number with 0.

2


Any number prefixed with 0 is considered octal. Octal numbers can only use digits 0 to 7, so 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

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

  • Thank you very much, I understand why the mistake

Browser other questions tagged

You are not signed in. Login or sign up in order to post.