为什么 Java 不认为整数是相等的?
我有应该相等的整数(并且我通过输出验证它)。但在我的 if
条件下,Java 没有看到这些变量具有相同的值。
我有以下代码:
if (pay[0]==point[0] && pay[1]==point[1]) {
game.log.fine(">>>>>> the same");
} else {
game.log.fine(">>>>>> different");
}
game.log.fine("Compare:" + pay[0] + "," + pay[1] + " -> " + point[0] + "," + point[1]);
它产生以下输出:
FINE: >>>>>> different
FINE: Compare:: 60,145 -> 60,145
可能我必须补充一点 point
是这样定义的:
Integer[] point = new Integer[2];
并且 pay
我们从循环构造函数中获取:
for (Integer[] pay : payoffs2exchanges.keySet())
所以,这两个变量都是整数类型。
I have integers that are supposed to be equal (and I verify it by output). But in my if
condition Java does not see these variables to have the same value.
I have the following code:
if (pay[0]==point[0] && pay[1]==point[1]) {
game.log.fine(">>>>>> the same");
} else {
game.log.fine(">>>>>> different");
}
game.log.fine("Compare:" + pay[0] + "," + pay[1] + " -> " + point[0] + "," + point[1]);
And it produce the following output:
FINE: >>>>>> different
FINE: Compare:: 60,145 -> 60,145
Probably I have to add that point
is defined like that:
Integer[] point = new Integer[2];
and pay
us taken from the loop-constructor:
for (Integer[] pay : payoffs2exchanges.keySet())
So, these two variables both have the integer type.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
查看这篇文章:装箱值和相等
比较包装类型,例如 <使用
==
或!=
的 code>Integer、Long
或Boolean
,您将它们作为参考进行比较,而不是作为值。如果两个变量指向不同的对象,则它们不会
==
彼此,即使对象代表相同的值。解决方案是使用
.equals()
比较值...…或者显式取消操作数装箱。
Check out this article: Boxed values and equality
When comparing wrapper types such as
Integer
s,Long
s orBoolean
s using==
or!=
, you're comparing them as references, not as values.If two variables point at different objects, they will not
==
each other, even if the objects represent the same value.The solution is to compare the values using
.equals()
……or to unbox the operands explicitly.
如果它们是简单的
int
类型,那么它就可以工作。对于
Integer
,在比较中使用.intValue()
或compareTo(Object other)
或equals(Object other)
。If they were simple
int
types, it would work.For
Integer
use.intValue()
orcompareTo(Object other)
orequals(Object other)
in your comparison.在java中,-128到127范围内的数值会被缓存,因此如果您尝试比较,
这会起作用,但是如果您尝试使用超出上述给定范围的数字,则需要使用 equals 方法进行值比较,因为“==” " 将检查两者是否是相同的对象而不是相同的值。
In java numeric values within range of -128 to 127 are cached so if you try to compare
this would work, but if you try with numbers out of the above give range they need to be compared with equals method for value comparison because "==" will check if both are same object not same value.
这里有两种类型需要区分:
int
,您大多数时间使用的原始整数类型,但不是对象类型Integer
,它是Integer
的对象包装器。 code>int 可用于在需要对象的 API 中使用整数There are two types to distinguish here:
int
, the primitive integer type which you use most of the time, but is not an object typeInteger
, an object wrapper around anint
which can be used to use integers in APIs that require objects当您尝试比较两个对象(并且 Integer 是一个对象,而不是变量)时,结果将始终是它们不相等,
在您的情况下,您应该比较对象的字段(在本例中为 intValue)
尝试声明 int变量而不是 Integer 对象,它会有所帮助
when you try to compare two objects (and an Integer is an object, not a variable) the result will always be that they're not equal,
in your case you should compare fields of the objects (in this case intValue)
try declaring int variables instead of Integer objects, it will help
at 表达式的条件
,使用相等运算符 == 来比较引用
与 a 引用是否相等。
一般情况下,当原始类型值(如 int、...)与 == 进行比较时,如果两者都满足,则结果为 true值是相同的。当引用(例如 Integer、String...)与 == 进行比较时,如果两个引用引用内存中的同一对象,则结果为 true。
要比较对象的实际内容(或状态信息)是否相等,必须调用方法。
因此,使用此
表达式您可以创建一个具有新引用的新对象并将其分配给点变量。
例如:
比较 a 和 b 使用:
因为它们都是原始类型值。
要将 a 与 c 进行比较,请使用:
因为自动装箱功能。
比较 c 和 e 使用:
因为 e 变量中有新引用。
比较 c 和 d 使用起来更好、更安全:
因为:
The condition at
expression, uses the equality operator == to compare a reference
for equality with the a reference
In general, when primitive-type values (such as int, ...) are compared with == , the result is true if both values are identical. When references (such as Integer, String, ...) are compared with == , the result is true if both references refer to the same object in memory.
To compare the actual contents (or state information) of objects for equality, a method must be invoked.
Thus, with this
expression you create a new object that has got new reference and assign it to point variable.
For example:
To compare a with b use:
because both of them are primitive-type values.
To compare a with c use:
because of auto-boxing feature.
for compare c with e use:
because of new reference in e variable.
for compare c with d it is better and safe to use:
because of: