为什么 Integer.MIN_VALUE 不等于存储在变量中的 Integer.MIN_VALUE?
Why isn't Integer.MIN_VALUE equal to stored Integer.MIN_VALUE in variable?
我用以下字段创建了一个间隔 class:
...
private static final Integer MINF = Integer.MIN_VALUE;
Integer head,tail;
...
当我创建这个 class 的实例时,创建 this.head = Integer.MIN_VALUE
,并且我想检查 head 的值是否等于 MINF
,它说它们不是'等于。
Interval i = new Interval(Integer.MIN_VALUE,10);
System.out.println(i.toString()); //[-2147483648,10]
所以我继续尝试打印值,
public String toString() {
...
//What the hell?
System.out.println("MINF == Integer.MIN_VALUE: " + (MINF == Integer.MIN_VALUE)); //true
System.out.println("MINF == this.head: " + (MINF == this.head)); //false
System.out.println("Integer.MIN_VALUE == this.head: " + (Integer.MIN_VALUE == this.head)); //true
...
return "*insert interval in format*";
}
也就是说
MINF == Integer.MIN_VALUE
是 true
MINF == this.head
是 false,尽管 this.head = -2147483648
Integer.MIN_VALUE == this.head
是 true
我是不是漏掉了什么,为什么第二个是假的?
整数是对象的包装 class、子对象并包含 int
值。
如果您只使用原始类型 int
,==
进行数值比较而不是对象地址比较。
注意 Integer.MIN_VALUE
当然也是 int
。
你忽略了这样一个事实,即当存储在 Integer 中(也就是说,你将 Integer.MIN_VALUE 存储在两个不同的整数中)并在它们之间使用 == 时,比较的不是值,而是对象.
对象不相同,因为它们是两个不同的对象。
当每个对象与 Integer.MIN_VALUE 进行比较时,由于 Integer.MIN_VALUE 是一个 int,因此对象会自动拆箱并使用 int 比较进行比较。
您正在使用 Integer
个对象。 ==
的使用应仅用作单个基元值的比较。由于您使用了 Integer
class 而不是原始 int
,因此它比较的是对象在两个变量之间的引用而不是它们的值。
因为 MINF
是 head
的一个单独对象,您使用 ==
.
进行直接比较会收到 false
这里没有人说明它们是不同对象的原因。显然:
System.out.println(new Integer(10) == new Integer(10));
输出错误,原因已在该问题的其他答案和 Comparing Integer objects
中讨论到死
但是,为什么会发生这种情况? 您似乎没有打电话给 new Integer
。原因是:
Integer.MIN_VALUE
returns 一个 int
,而不是一个 Integer
.
- 您已将
MINF
定义为 Integer
- 自动装箱使用
valueOf
。参见 Does autoboxing call valueOf()?
如果 int
不在 integer cache 中,valueOf
调用 new Integer
,
- 缓存仅包含
-128 -> 127
个值。
而 是您看到 "two Integer
objects are not ==
behavior" 的原因,因为 自动装箱。 自动装箱也是为什么不相等的原因在这里似乎是可传递的。
您可以通过以下方式解决此问题:
private static final int MINF = Integer.MIN_VALUE;
并且,一般来说:不要对简单字段使用 Integer
。;仅在您 实际需要对象 .
时将其用作通用类型
我用以下字段创建了一个间隔 class:
...
private static final Integer MINF = Integer.MIN_VALUE;
Integer head,tail;
...
当我创建这个 class 的实例时,创建 this.head = Integer.MIN_VALUE
,并且我想检查 head 的值是否等于 MINF
,它说它们不是'等于。
Interval i = new Interval(Integer.MIN_VALUE,10);
System.out.println(i.toString()); //[-2147483648,10]
所以我继续尝试打印值,
public String toString() {
...
//What the hell?
System.out.println("MINF == Integer.MIN_VALUE: " + (MINF == Integer.MIN_VALUE)); //true
System.out.println("MINF == this.head: " + (MINF == this.head)); //false
System.out.println("Integer.MIN_VALUE == this.head: " + (Integer.MIN_VALUE == this.head)); //true
...
return "*insert interval in format*";
}
也就是说
MINF == Integer.MIN_VALUE
是 true
MINF == this.head
是 false,尽管 this.head = -2147483648
Integer.MIN_VALUE == this.head
是 true
我是不是漏掉了什么,为什么第二个是假的?
整数是对象的包装 class、子对象并包含 int
值。
如果您只使用原始类型 int
,==
进行数值比较而不是对象地址比较。
注意 Integer.MIN_VALUE
当然也是 int
。
你忽略了这样一个事实,即当存储在 Integer 中(也就是说,你将 Integer.MIN_VALUE 存储在两个不同的整数中)并在它们之间使用 == 时,比较的不是值,而是对象. 对象不相同,因为它们是两个不同的对象。 当每个对象与 Integer.MIN_VALUE 进行比较时,由于 Integer.MIN_VALUE 是一个 int,因此对象会自动拆箱并使用 int 比较进行比较。
您正在使用 Integer
个对象。 ==
的使用应仅用作单个基元值的比较。由于您使用了 Integer
class 而不是原始 int
,因此它比较的是对象在两个变量之间的引用而不是它们的值。
因为 MINF
是 head
的一个单独对象,您使用 ==
.
这里没有人说明它们是不同对象的原因。显然:
System.out.println(new Integer(10) == new Integer(10));
输出错误,原因已在该问题的其他答案和 Comparing Integer objects
中讨论到死但是,为什么会发生这种情况? 您似乎没有打电话给 new Integer
。原因是:
Integer.MIN_VALUE
returns 一个int
,而不是一个Integer
.- 您已将
MINF
定义为Integer
- 自动装箱使用
valueOf
。参见 Does autoboxing call valueOf()?
如果 valueOf
调用new Integer
,- 缓存仅包含
-128 -> 127
个值。
- 缓存仅包含
int
不在 integer cache 中,而 是您看到 "two Integer
objects are not ==
behavior" 的原因,因为 自动装箱。 自动装箱也是为什么不相等的原因在这里似乎是可传递的。
您可以通过以下方式解决此问题:
private static final int MINF = Integer.MIN_VALUE;
并且,一般来说:不要对简单字段使用 Integer
。;仅在您 实际需要对象 .