为什么 Java 不认为整数是相等的?

发布于 2024-10-07 05:14:53 字数 749 浏览 0 评论 0原文

我有应该相等的整数(并且我通过输出验证它)。但在我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

就是爱搞怪 2024-10-14 05:14:53

查看这篇文章:装箱值和相等

比较包装类型,例如 <使用 ==!= 的 code>Integer、LongBoolean,您将它们作为参考进行比较,而不是作为值。

如果两个变量指向不同的对象,则它们不会==彼此,即使对象代表相同的值

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

整数 i = new Integer(10);
整数 j = 新整数(10);
System.out.println(i == j); // 错误的
System.out.println(i != j); // 真的

解决方案是使用 .equals() 比较值...

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

整数 i = new Integer(10);
整数 j = 新整数(10);
System.out.println(i.equals(j)); // 真的

…或者显式取消操作数装箱。

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

整数 i = new Integer(10);
整数 j = 新整数(10);
System.out.println((int) i == (int) j); // 真的

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.

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

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

Example: Compare objects using .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

寄风 2024-10-14 05:14:53

如果它们是简单的 int 类型,那么它就可以工作。

对于Integer,在比较中使用.intValue()compareTo(Object other)equals(Object other)

If they were simple int types, it would work.

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

━╋う一瞬間旳綻放 2024-10-14 05:14:53

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

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

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

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

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.

美人如玉 2024-10-14 05:14:53

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

  • 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 type
  • Integer, an object wrapper around an int which can be used to use integers in APIs that require objects
〃安静 2024-10-14 05:14:53

当您尝试比较两个对象(并且 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

柏拉图鍀咏恒 2024-10-14 05:14:53

at 表达式的条件

pay[0]==point[0]

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

Integer pay[0]

与 a 引用是否相等。

Integer point[0]

一般情况下,当原始类型值(如 int、...)与 == 进行比较时,如果两者都满足,则结果为 true值是相同的。当引用(例如 Integer、String...)与 == 进行比较时,如果两个引用引用内存中的同一对象,则结果为 true。
要比较对象的实际内容(或状态信息)是否相等,必须调用方法。
因此,使用此

Integer[] point = new Integer[2];

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

例如:

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

比较 a 和 b 使用:

a == b

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

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

a == c

因为自动装箱功能。

比较 c 和 e 使用:

c.equals(e)

因为 e 变量中有新引用。

比较 c 和 d 使用起来更好、更安全:

  c.equals(d)

因为:

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

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

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

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

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

because both of them are primitive-type values.

To compare a with c use:

a == c

because of auto-boxing feature.

for compare c with e use:

c.equals(e)

because of new reference in e variable.

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

  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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文