简体   繁体   中英

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

I made an Interval class with the following fields:

...
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.

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 == this.head is false , although this.head = -2147483648

Integer.MIN_VALUE == this.head is true

Am I missing something for why the second one is false?

Integer is the wrapping class, child of Object and containing an int value.

If you use only the primitive type int , == does a numerical comparison and not an object address comparison.

Mind that Integer.MIN_VALUE of course is an int too.

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. 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.

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

But, why is that happening here? You don't appear to be calling new Integer . The reason is that:

  1. Integer.MIN_VALUE returns an int , not an Integer .
  2. You have defined MINF to be an Integer
  3. Autoboxing uses valueOf . See Does autoboxing call valueOf()?
  4. valueOf calls new Integer if the int is not in the integer cache ,
    • The cache is only the values -128 -> 127 inclusive.

And that is why you are seeing the "two Integer objects are not == behavior", because of autoboxing. 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. ; only use it as a generic type where you actually need the object .

You are using Integer objects. 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.

Because MINF is a separate object to head you are receiving false for a direct comparison using == .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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