[英]Java: why the “long” primitive type does not accept a simple number?
I got a method that receives a long
type parameher, and I try to call it passing 1
: 我有一个接收
long
类型参数的方法,我试着称它传递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? 但是,是不是
1
较长的数字呢? Isn't it inside the long scope ?? 是不是在长范围内 ?
PS: Just to say, I solved the problem with this code: PS:只是说,我用这段代码解决了这个问题:
Integer y = 1;
long x = y.longValue();
contato.setId(x);
It's just a didatic question. 这只是一个狡猾的问题。
You should use contato.setId(1L);
你应该使用
contato.setId(1L);
(notice the "L" suffix) (注意“L”后缀)
The literal "1" represents a primitive int
value, which is casted to an java.lang.Integer
wrapper class. 文字“1”表示原始
int
值,它被转换为java.lang.Integer
包装类。
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
. long
是一个包含64位的数据类型(不要与Object Long
混淆!)与int(32位),因此您不能使用从int
到long
的简单赋值。 See: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html 请参阅: 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
应该用数字后跟L
来声明,例如:
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!
因此,例如,如果你将在一个循环(多次)中执行它,你的代码将在数量级上变慢!
Long
is not a primitive type, long
is. Long
不是原始类型, long
是。 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: 使用包装类而不是基元类型时,需要通过添加
L
后缀向编译器明确指示传递的参数是long
:
contato.setId(1L);
Or you can simply change the setId
method so that it takes a primitive long
argument instead. 或者您可以简单地更改
setId
方法,以便它采用原始的long
参数。
setId
takes a capital-L Long
, which is Java's wrapper around lowercase-l long
(AKA 64-bit integer). setId
采用大写L Long
,这是Java的小写l long
(AKA 64位整数)的包装器。 Because of this, Java easily knows how to convert a long
to a Long
without you doing anything special. 因此,Java很容易知道如何在不做任何特殊操作的情况下将
long
转换为Long
。 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
. 所以,就像其他答案一样,你可以简单地做
setId(1L)
,它给它一个long
,很容易转换为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. 但是,如果必须使用32位
int
,则必须先将其转换为long
或Long
,以便Java知道如何处理它。 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
). 你看,Java并不知道如何将小写-i
int
转换为大写-L Long
,只知道大写-I Integer
( int
周围的包装类)。
So, assuming your int's name is i
, you can use these as well: 所以,假设你的int的名字是
i
,你也可以使用它们:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.