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

Asked

Viewed 1,181 times

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 error on line 4. Error: The literal 63636363555 of type int is out of range

But the kind that method cpfIsValid receive is a long, not an int.

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

Why is this happening?

2 answers

6


This is because the number you are passing is considered by the compiler to be a literal value of the type int, and as a whole, it exceeds the valid number range of this type. If you want to pass a literal numeric value of the type long, need to explicitly point to the compiler by adding a l at 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 the proof in ideone : https://ideone.com/aQHbcm

3

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

System.out.println(c.cpfIsValid(63636363555L));

Browser other questions tagged

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