Compile error: The literal ... of type int is out of range

2
    CadastroDePessoasFisicas c2 = new CadastroDePessoasFisicas("636.363.635");
    System.out.println(c2.getNumero());
    System.out.println(c2.getNumeroValidador());
    System.out.println(c.cpfIsValid(63636363555));
    System.out.println("\n");

Eclipse is reporting an error on line 4. Error: The literal 63636363555 of type int is out of range

But the type that the cpfIsValid method receives is a long, not an int.

public boolean cpfIsValid(long cpf) { 
// código 
}

Why is this happening?

    
asked by anonymous 13.05.2018 / 23:33

2 answers

7

This is because the number you are passing is considered by the compiler as a literal value of type int , and as integer, it exceeds range of valid numbers of this type. If you want to pass a literal numeric value of type long , you need to explicitly point to the compiler by adding a l to the end of the literal value, otherwise the compiler will consider int and this error will occur.

The simple addition of l solves the problem:

CadastroDePessoasFisicas c2 = new CadastroDePessoasFisicas("636.363.635");
System.out.println(c2.getNumero());
System.out.println(c2.getNumeroValidador());
System.out.println(c.cpfIsValid(63636363555l));
System.out.println("\n");

See proof at ideone: link

    
13.05.2018 / 23:44
3

The compiler is trying to parse the value of long as an int type, in case you have to declare it as follows:

System.out.println(c.cpfIsValid(63636363555L));
    
13.05.2018 / 23:45