Java: why the “long” primitive type does not accept a simple number?

烈酒焚心 提交于 2019-12-07 03:48:10

问题


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.


回答1:


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!




回答2:


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.




回答3:


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.




回答4:


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


来源:https://stackoverflow.com/questions/28353137/java-why-the-long-primitive-type-does-not-accept-a-simple-number

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!