比较java中的两个原始长变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9690981/
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
Compare two primitive long variables in java
提问by Dragan
The title is pretty self-explanatory. I'm moving from C# to Java. I have an object and a getter method which returns its ID. I want to compare the ids of two objects of the same type and check if the values of their ids are equal.
标题是不言自明的。我正在从 C# 转向 Java。我有一个对象和一个返回其 ID 的 getter 方法。我想比较两个相同类型的对象的 id 并检查它们的 id 值是否相等。
tried:
试过:
obj.getId() == obj1.getId();
Long id1 = obj.getId();
Long id2 = obj1.getId();
assertTrue(id1.equals(id2))
assertTrue(id1== id2)
采纳答案by Bohemian
In java:
在Java中:
- the
==
operator tells you if the two operands are the sameobject (instance). - the
.equals()
method onLong
tells you if they are equal in value.
- 该
==
运营商告诉你,如果两个操作数是同一个对象(实例)。 - 在
.equals()
方法上Long
告诉你,如果他们的价值相等。
But you shouldn't do either. The correct way to do it is this:
但你也不应该这样做。正确的做法是这样的:
assertEquals(id1, id2);
With assertEquals()
, if the assertion fails, the error message will tell you what the two values were, eg expected 2, but was 5
etc
有了assertEquals()
,如果断言失败,错误信息会告诉你这两个值分别为,如expected 2, but was 5
等
回答by Jon Skeet
You need to be aware of the difference between Long
and long
- long
is the primitive type, Long
is the wrapper type. (A bit like a boxed value in C#, but strongly typed.) What's the return type of getId()
?
您需要了解Long
和long
-long
是原始类型,Long
是包装类型之间的区别。(有点像 C# 中的装箱值,但是是强类型的。) 的返回类型是getId()
什么?
Simply:
简单地:
assertEqual(id1, id2);
should be fine if you're doing this in a test. Otherwise, you could use:
如果你在测试中这样做应该没问题。否则,您可以使用:
if (id1.equals(ids2))
if they're definitely notnull, or use Guava:
如果它们绝对不是空的,或者使用Guava:
if (Objects.equal(id1, id2))
to handle nullity. (You can write Objects.equal
yourself, of course, but you should definitely get hold of Guava anyway, so you might as well use that...)
处理无效。(Objects.equal
当然你可以自己写,但无论如何你一定要掌握 Guava ,所以你也可以使用它......)
It's worth noting that certain wrapper objects are reused - so for example:
值得注意的是,某些包装器对象被重用 - 例如:
// This will work
Long x = 5L;
Long y = 5L;
assertTrue(x == y); // Reference comparison
// This *probably* won't but it could!
x = 10000L;
y = 10000L;
assertTrue(x == y); // Reference comparison
回答by bsimic
Try doing the following:
尝试执行以下操作:
assertTrue(id1.longValue() == id2.longValue())
回答by edthethird
in java,
在 Java 中,
use ==
to compare primitives and x.equals(y)
to compare objects.
用于==
比较基元和x.equals(y)
比较对象。
I'm not sure what your question is-- it looks like you tried both, and did neither work?
我不确定您的问题是什么 - 看起来您都尝试过,但都没有奏效?
Note, you are using Long
instead of long
, so you want to use .equals()
请注意,您使用的是Long
而不是long
,因此您要使用.equals()
(long
is a primitive, Long
is an object)
(long
是一个原始,Long
是一个对象)
回答by spidey
They must not be equal using ==. Use either:
它们不能使用 == 相等。使用:
id1.equals(id2)
or
或者
id1.longValue() == id2.longValue()
In both cases I left out null-checks because I doubt you'd forget them, if they were needed here ;)
在这两种情况下,我都省略了空检查,因为我怀疑如果这里需要它们,您会忘记它们;)
So, why is Long not always == another Long with the same value? The answer is easy. They're different objects! Depending on howthe Long was retrieved they might be the same, because of internal caching. See Long.valueOf.
那么,为什么 Long 并不总是 == 另一个具有相同值的 Long 呢?答案很简单。他们是不同的对象!根据怎样的龙被检索到他们可能是一样的,因为内部缓存。见Long.valueOf。
回答by Jochen
In addition to all the comments about the right comparison, strong typing can help you resolve your issue.
除了关于正确比较的所有评论之外,强类型还可以帮助您解决问题。
Does getID() return something of type Long (that's a class type) or of type long (that's a primitive type)? The problem is that if the method returns a primitive value, then comparing with '==' and 'equals()' will both work, because Java automatically casts primitives into wrapper objects when you need them.
getID() 是否返回 Long 类型(即类类型)或 long 类型(即原始类型)的内容?问题在于,如果该方法返回一个原始值,那么与 '==' 和 'equals()' 进行比较都将起作用,因为 Java 会在您需要它们时自动将原始值转换为包装对象。
But the other way, if getID() returns a Long value, comparing with '==' is likely to fail, because that checks if you have the same object, not if the objects have the same value.
但另一方面,如果 getID() 返回一个 Long 值,则与 '==' 进行比较可能会失败,因为它会检查您是否具有相同的对象,而不是这些对象是否具有相同的值。
回答by Another_Dev
To compare two primitive long you can simply use ==
要比较两个原始 long 你可以简单地使用 ==
Example:
例子:
long x = 1L;
long y = 1L;
if (x == y) {
System.out.println("value of x and y are same");
}
To compare two Long objects you can use Long.compare(long x, long y). This method was added in java 1.7. Below is the method implementation:
要比较两个 Long 对象,您可以使用 Long.compare(long x, long y)。这个方法是在 java 1.7 中添加的。下面是方法实现:
public static int compare(long x, long y) {
return (x < y) ? -1 : ((x == y) ? 0 : 1);
}
Example:
例子:
Long x = new Long(1);
Long y = new Long(1);
if (Long.compare(x,y) == 0) {
System.out.println(values of x and y are same);
}