6

I got a method that receives a long type parameher, and I try to call it passing 1:

contato.setId(1);

And I receive this:

The method setId(Long) in the type Contato is not applicable for the arguments (int).

But, isn't 1 a long number as well? Isn't it inside the long scope??

PS: Just to say, I solved the problem with this code:

Integer y = 1;
long x = y.longValue();
contato.setId(x);

It's just a didatic question.

Mr Guliarte
  • 739
  • 1
  • 10
  • 27
  • 2
    If the method accepted a little-l `long`, it'd work just fine; it's just that int literals can't autobox directly to `Long`. – Louis Wasserman Feb 05 '15 at 20:14
  • possible duplicate of [How did a float turn into a double here?](http://stackoverflow.com/q/3680879/217324) - my answer there explains the decisions behind the autoboxing limitation mentioned in Louis' comment – Nathan Hughes Feb 05 '15 at 20:34

4 Answers4

7

You should use contato.setId(1L); (notice the "L" suffix)

The literal "1" represents a primitive int value, which is casted to an java.lang.Integer wrapper class.

rmuller
  • 12,062
  • 4
  • 64
  • 92
6

long is a datatype that contains 64bits (not to be confused with the Object Long!) vs. an int (32 bits), so you can't use a simple assignment from int to long. See: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

In order to see how to declare the various datatypes, you should check specifically the following table:

Datatype    Default Value
byte        0
short       0
int         0
long        0L
float       0.0f
double      0.0d
char        '\u0000'
Object      null
boolean     false

So, for your case, long should be declared with the number followed by an L, for instance:

long x = 100L;

Further, doing what you're doing with autoboxing:

Integer y = 1;
long x = y.longValue();

is not only unnecessary - it's very wasteful as well. So, for example, if you'll do it in a loop (many times) your code will be slower in order of magnitude!

Ky -
  • 30,724
  • 51
  • 192
  • 308
Nir Alfasi
  • 53,191
  • 11
  • 86
  • 129
2

Long is not a primitive type, long is. When using the wrapper classes instead of the primitive types, you need to explicitly indicate to the compiler that the passed argument is a long by adding the L suffix:

contato.setId(1L);

Or you can simply change the setId method so that it takes a primitive long argument instead.

M A
  • 71,713
  • 13
  • 134
  • 174
  • 1
    There is also a possibility that the capital "L" on the setId argument type is a typo, and the correct fix is to make it take a primitive `long`. – Patricia Shanahan Feb 05 '15 at 20:35
0

setId takes a capital-L Long, which is Java's wrapper around lowercase-l long (AKA 64-bit integer). Because of this, Java easily knows how to convert a long to a Long without you doing anything special. So, like the other answers say, you could simply do setId(1L), which is giving it a long, which it easily converts to a Long.

However, if you must use a 32-bit int, you must first convert it to a long or a Long, so Java knows how to handle it. You see, Java does not know implicitly how to convert a lowercase-i int to an uppercase-L Long, only to an uppercase-I Integer (the wrapper class around int).

So, assuming your int's name is i, you can use these as well:

setId((long)i);         // Cast your int to a long, which Java can turn into a Long
setId((Long)(long)i);   // Cast your int to a long, then that long to a Long
setId(new Long(i));     // Create a new Long object based on your int
setId(Long.valueOf(i)); // Get the Long version of your int
Ky -
  • 30,724
  • 51
  • 192
  • 308