简体   繁体   English

为什么Integer.MIN_VALUE不等于变量中存储的Integer.MIN_VALUE?

[英]Why isn't Integer.MIN_VALUE equal to stored Integer.MIN_VALUE in variable?

I made an Interval class with the following fields: 我使用以下字段制作了一个Interval类:

...
private static final Integer MINF = Integer.MIN_VALUE;
Integer head,tail;
...

when I make an instance of this class, making this.head = Integer.MIN_VALUE , and I want to check if the value of head is equal to MINF , it says that they aren't equal. 当我创建此类的实例时,使this.head = Integer.MIN_VALUE ,并且我想检查head的值是否等于MINF ,它表示它们不相等。

Interval i = new Interval(Integer.MIN_VALUE,10);
System.out.println(i.toString()); //[-2147483648,10]

So I went ahead and tried to print the values, 所以我继续尝试打印值,

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*";
} 

Which says 哪说

MINF == Integer.MIN_VALUE is true MINF == Integer.MIN_VALUEtrue

MINF == this.head is false , although this.head = -2147483648 MINF == this.headfalse ,尽管this.head = -2147483648

Integer.MIN_VALUE == this.head is true Integer.MIN_VALUE == this.headtrue

Am I missing something for why the second one is false? 我是否因为第二个错误的原因而错过了某些东西?

Integer is the wrapping class, child of Object and containing an int value. Integer是包装类,是Object的子级,并且包含int值。

If you use only the primitive type int , == does a numerical comparison and not an object address comparison. 如果仅使用基本类型int ,则==进行数值比较,而不进行对象地址比较。

Mind that Integer.MIN_VALUE of course is an int too. 请注意, Integer.MIN_VALUE当然也是一个int

You are missing the fact that when stored in Integer (that is, you store Integer.MIN_VALUE in two different integers) and using == between them, the comparison is not of the values, but of the objects. 您缺少这样一个事实:当将其存储在Integer中(即,将Integer.MIN_VALUE存储在两个不同的整数中)并且在它们之间使用==时,比较不是值的比较,而是对象的比较。 The objects are not identical because they are two different objects. 对象不同,因为它们是两个不同的对象。 When each object is compared to Integer.MIN_VALUE, since Integer.MIN_VALUE is an int, the object is autounboxed and compared using int comparison. 当将每个对象与Integer.MIN_VALUE进行比较时,由于Integer.MIN_VALUE是一个int,因此该对象将自动拆箱并使用int比较进行比较。

No one here has addressed the REASON why they're different objects. 这里没有人提到原因,为什么它们是不同的对象。 Obviously: 明显:

System.out.println(new Integer(10) == new Integer(10));

outputs false, for reasons that have been discussed to death in the other answers to this question and in Comparing Integer objects 输出false,原因是在此问题的其他答案以及“ 比较整数”对象中已讨论到死的原因

But, why is that happening here? 但是,为什么会在这里发生呢? You don't appear to be calling new Integer . 您似乎没有在调用new Integer The reason is that: 原因是:

  1. Integer.MIN_VALUE returns an int , not an Integer . Integer.MIN_VALUE返回一个int ,而不是Integer
  2. You have defined MINF to be an Integer 您已将MINF定义为Integer
  3. Autoboxing uses valueOf . 自动装箱使用valueOf See Does autoboxing call valueOf()? 请参阅自动装箱是否调用valueOf()?
  4. valueOf calls new Integer if the int is not in the integer cache , 如果int不在整数缓存中 ,则valueOf调用new Integer
    • The cache is only the values -128 -> 127 inclusive. 缓存仅是值-128 -> 127含)。

And that is why you are seeing the "two Integer objects are not == behavior", because of autoboxing. 就是为什么由于自动装箱而看到“两个Integer对象不是==行为”的原因 Autoboxing is also why equality does not appear to be transitive here. 自动装箱也是为什么平等在这里似乎不具有传递性的原因。

You can fix this problem by instead using: 您可以改用以下方法解决此问题:

private static final int MINF = Integer.MIN_VALUE;

And, in general: don't use Integer for simple fields. 而且,通常: 不要将Integer用于简单字段。 ; ; only use it as a generic type where you actually need the object . 仅将它用作您实际需要该对象的泛型类型。

You are using Integer objects. 您正在使用Integer对象。 The use of == should be used as a comparison of individual primitive's values only. ==的使用应仅用作单个原始值的比较。 Since you used the Integer class rather than the primitive int then it is comparing the object's references between the two variables rather than their values. 由于您使用的是Integer类而不是原始的int因此它将在两个变量之间而不是它们的值之间比较对象的引用。

Because MINF is a separate object to head you are receiving false for a direct comparison using == . 由于MINF是一个单独的对象,以head您所使用的直接比较接受虚假==

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM