为什么 Java 看不到整数相等?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4428774/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 17:21:18  来源:igfitidea点击:

Why Java does not see that Integers are equal?

javacomparisonif-statementequals

提问by Roman

I have integers that are supposed to be equal (and I verify it by output). But in my ifcondition Java does not see these variables to have the same value.

我有应该相等的整数(我通过输出验证它)。但在我的if情况下,Java 没有看到这些变量具有相同的值。

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 pointis defined like that:

可能我必须补充一点,它point是这样定义的:

Integer[] point = new Integer[2];

and payus taken from the loop-constructor:

并且pay我们从环路构造采取:

for (Integer[] pay : payoffs2exchanges.keySet())

So, these two variables both have the integer type.

因此,这两个变量都具有整数类型。

采纳答案by aioobe

Check out this article: Boxed values and equality

查看这篇文章:盒装值和平等

When comparing wrapper types such as Integers, Longs or Booleans using ==or !=, you're comparing them as references, not as values.

使用or比较Integers、Longs 或Booleans等包装类型时,您将它们作为引用进行比较,而不是作为值进行比较==!=

If two variables point at different objects, they will not ==each other, even if the objects represent the same value.

如果两个变量指向不同的对象,即使对象代表相同的值,它们也不会==相互关联。

Example:Comparing different Integer objects using ==and !=.

Integer i = new Integer(10);
Integer j = new Integer(10);
System.out.println(i == j); // false
System.out.println(i != j); // true

示例:使用==和比较不同的 Integer 对象!=

Integer i = new Integer(10);
Integer j = new Integer(10);
System.out.println(i == j); // false
System.out.println(i != j); // true

The solution is to compare the values using .equals()

解决方案是使用.equals()

Example:Compare objects using .equals(…)

Integer i = new Integer(10);
Integer j = new Integer(10);
System.out.println(i.equals(j)); // true

示例:比较对象使用.equals(…)

Integer i = new Integer(10);
Integer j = new Integer(10);
System.out.println(i.equals(j)); // true

…or to unbox the operands explicitly.

...或显式拆箱操作数。

Example:Force unboxing by casting:

Integer i = new Integer(10);
Integer j = new Integer(10);
System.out.println((int) i == (int) j); // true

示例:通过强制转换强制拆箱:

Integer i = new Integer(10);
Integer j = new Integer(10);
System.out.println((int) i == (int) j); // true



References / further reading

参考资料/进一步阅读

回答by sje397

If they were simple inttypes, it would work.

如果它们是简单int类型,它会起作用。

For Integeruse .intValue()or compareTo(Object other)or equals(Object other)in your comparison.

对于Integer使用.intValue()compareTo(Object other)equals(Object other)在您的比较。

回答by Bart van Heukelom

There are two types to distinguish here:

这里有两种类型需要区分:

  • int, the primitive integer type which you use most of the time, but is not an object type
  • Integer, an object wrapper around an intwhich can be used to use integers in APIs that require objects
  • int,您大部分时间使用的原始整数类型,但不是对象类型
  • Integer,一个围绕 an 的对象包装器int,可用于在需要对象的 API 中使用整数

回答by Andrzej Bobak

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)

在您的情况下,您应该比较对象的字段(在本例中为 intValue)

try declaring int variables instead of Integer objects, it will help

尝试声明 int 变量而不是 Integer 对象,它会有所帮助

回答by Nayan Srivastava

In java numeric values within range of -128 to 127 are cached so if you try to compare

在 java 中,-128 到 127 范围内的数值被缓存,因此如果您尝试比较

Integer i=12 ;
Integer j=12 ; // j is pointing to same object as i do.
if(i==j)
   print "true";

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.

这会起作用,但是如果您尝试使用超出上述给定范围的数字,则需要将它们与 equals 方法进行比较以进行值比较,因为“==”将检查两者是否是相同的对象而不是相同的值。

回答by MMKarami

The condition at

条件在

pay[0]==point[0]

expression, uses the equality operator == to compare a reference

表达式,使用相等运算符 == 来比较引用

Integer pay[0]

for equality with the a reference

与引用相等

Integer point[0]

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

通常,当原始类型的值(例如 int、...)与 == 进行比较时,如果两个值相同,则结果为真。当引用(例如 Integer、String 等)与 == 进行比较时,如果两个引用都引用内存中的同一个对象,则结果为真。为了比较对象的实际内容(或状态信息)是否相等,必须调用一个方法。因此,有了这个

Integer[] point = new Integer[2];

expression you create a new object that has got new reference and assign it to point variable.

表达式创建一个新对象,该对象具有新引用并将其分配给点变量。

For example:

例如:

int a = 1;
int b = 1;
Integer c = 1;
Integer d = 1;
Integer e = new Integer(1);

To compare a with b use:

比较 a 和 b 使用:

a == b

because both of them are primitive-type values.

因为它们都是原始类型的值。

To compare a with c use:

要将 a 与 c 进行比较,请使用:

a == c

because of auto-boxing feature.

由于自动装箱功能。

for compare c with e use:

比较 c 和 e 使用:

c.equals(e)

because of new reference in e variable.

因为 e 变量中有新的引用。

for compare c with d it is better and safe to use:

为了将 c 与 d 进行比较,使用更好更安全:

  c.equals(d)

because of:

因为:

As you know, the == operator, applied to wrapper objects, only tests whether the objects have identical memory locations. The following comparison would therefore probably fail:

如您所知,应用于包装对象的 == 运算符仅测试对象是否具有相同的内存位置。因此,以下比较可能会失败:

Integer a = 1000;
Integer b = 1000;
if (a == b) . . .

However, a Java implementation may, if it chooses, wrap commonly occurring values into identical objects, and thus the comparison might succeed. This ambiguity is not what you want. The remedy is to call the equals method when comparing wrapper objects.

但是,如果选择,Java 实现可以将常见的值包装到相同的对象中,因此比较可能会成功。这种歧义不是你想要的。补救方法是在比较包装对象时调用 equals 方法。