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.
If they were simple
int
types, it would work.For
Integer
use.intValue()
orcompareTo(Object other)
orequals(Object other)
in your comparison.In java numeric values within range of -128 to 127 are cached so if you try to compare
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.
Objects (such as
Integer
s) should not be compared through==
, but through.equals()
.What's important to understand is that several different
Integer
objects can represent the same int value. When your program prints>>> different
it simply says that the first object is not the same object as the second object. (While you probably want to compare the objects based on which value they represent.)From the official guide on autoboxing:
It may be worth noting that autoboxing is guaranteed to return the same object for integral values in the range [-128, 127], but an implementation may, at its discretion, cache values outside of that range.
My general recommendation is to use
int
instead ofInteger
for all local / member variables. In this particular case you seem to store coordinates in a 2-element array. I would suggest that you encapsulate this in aCoordinates
class or similar and override the equals method (and hashCode) in here.See also
There are two types to distinguish here:
int
, the primitive integer type which you use most of the time, but is not an object typeInteger
, an object wrapper around anint
which can be used to use integers in APIs that require objectsThe condition at
expression, uses the equality operator == to compare a reference
for equality with the a reference
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
expression you create a new object that has got new reference and assign it to point variable.
For example:
To compare a with b use:
because both of them are primitive-type values.
To compare a with c use:
because of auto-boxing feature.
for compare c with e use:
because of new reference in e variable.
for compare c with d it is better and safe to use:
because of:
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