Reputation: 906
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?
Upvotes: 1
Views: 796
Reputation: 32343
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:
Integer.MIN_VALUE
returns an int
, not an Integer
.MINF
to be an Integer
valueOf
. See Does autoboxing call valueOf()?valueOf
calls new Integer
if the int
is not in the integer cache,
-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.
Upvotes: 3
Reputation: 753
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 ==
.
Upvotes: 0
Reputation: 494
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.
Upvotes: 6
Reputation: 109613
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.
Upvotes: 8