Java新长(0)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33673323/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java new long(0)
提问by Arnold Cristobal
In my class I have this parameter:
在我的课上,我有这个参数:
public Long label_id ;
When I try to evaluate the value from label_id which is 0
当我尝试评估 label_id 的值为 0 时
if(item.label_id == new Long(0)) {
Doesn't enter here
} else {
Enters here
}
It is supposed to enter the condition since both are zero but it enters the else condition. I even tried debugging the code:
它应该进入条件,因为两者都为零,但它进入了 else 条件。我什至尝试调试代码:
label_id Long (id=142)
value 0
Am I missing something here?
我在这里错过了什么吗?
采纳答案by Gamerb
I am going to improve upon the answers by @Rocket and @ParkerHalo to explain why the new Long(0)
doesn't work.
我将改进@Rocket 和@ParkerHalo 的答案,以解释为什么new Long(0)
不起作用。
So the problem with the if line of code is that the new Long(0)
creates an object of class Long
, however you aren't compairing classes, want you are comparing are primitive types. Primitive types have no methods and are basally a set amount of memory dedicated to storing a number (Read the comments). They were created with C and most (if not all) made their way into Java. However the Long
Class has various methods to make working with longs easier in some cases.
所以 if 代码行的问题在于它new Long(0)
创建了一个 class 对象Long
,但是您不是在比较类,而是希望您比较的是原始类型。原始类型没有方法,基本上是一组专用于存储数字的内存(阅读评论)。它们是用 C 创建的,并且大多数(如果不是全部)进入了 Java。然而,Long
在某些情况下,类有多种方法可以使处理多头更容易。
You cannot compare them in this way because the new Long(0)
returns a reference to a class, not a primitive type.
您不能以这种方式比较它们,因为它new Long(0)
返回对类的引用,而不是原始类型。
The quickest way to get your code up and running is to do as @ParkerHalo suggested and compare the value returned to a primitive long with the value of Zero. So if you wanted to see if it was Zero you would use:
启动并运行代码的最快方法是按照@ParkerHalo 的建议进行操作,并将返回到原始 long 的值与零值进行比较。因此,如果您想查看它是否为零,您可以使用:
if(item.label_id.longValue() == 0L) {
if(item.label_id.longValue() == 0L) {
And you can replace the 0
with other values, as long as you keep the L
for longs.
并且您可以将 替换为0
其他值,只要您保持L
long 即可。
Hope this helps!
希望这可以帮助!
回答by Rocket
You are using an object comparison. and with new ... you generate a new object. the both objects are not the same... You can use new Long(0).equals(...)
您正在使用对象比较。并使用 new ... 生成一个新对象。这两个对象不一样...您可以使用 new Long(0).equals(...)
回答by ParkerHalo
You should extract the value of label_id
first and then compare it:
您应该首先提取的值,label_id
然后比较它:
if(item.label_id.longValue() == 0L)
回答by J?rn Buitink
use
用
item.label_id.longValue() == new Long(0).longValue()
or
或者
item.label_id.equals(new Long(0))
Otherwise you compare memory adresses / pointers
否则你比较内存地址/指针
回答by FrontierPsychiatrist
The == operator in Java checks for object equality. With new Long(0)
you explicitly create a new object that will never have the same identity as any other Long.
Java 中的 == 运算符检查对象是否相等。与new Long(0)
您显式地创建一个新对象,该对象永远不会与任何其他 Long 具有相同的身份。
Use the equals
method, but beware of NullPointerExceptions`.
使用该equals
方法,但要注意 NullPointerExceptions`。
if(item.label_id.equals(new Long(0))) {
} else {
}
You could also try to rely on the cache, but I wouldn't do that
你也可以尝试依赖缓存,但我不会那样做
if(item.label_id == Long.valueOf(0)) {
} else {
}