A problem on using x==Y or (x-y)==0 to control the if condition
My program includes the following code segment, which do sth based on whether two variables, temp.get(j1) and temp(j2) are equivalent or not.
for (int j1 =0; j1<2;j1++)
{
for (int j2 =0; j2<2;j2++)
{
System.out.println("j1="+j1+"j2="+j2+" "+temp1.get(j1)+"----"+temp2.get(j2));
int xyz = temp1.get(j1)-temp2.get(j2);
System.out.println("the difference is "+ xyz);
if (temp1.get(j1)==temp2.get(j2))
{
System.out.println("find match");
}
}
}
The program prints out sth like
j1=0j2=0 7698380----7698380
the difference is 0
Even the two values, temp1.get(j1) and temp2.get(j2) do overlap, the "if" part just did not go through. If I change the if (temp1.get(j1)==temp2.get(j2)) to
if (xyz == 0)
Then the result will look like
j1=0j2=0 7698380----7698380
the difference is 0
find match `
I think the two conditions for controlling the if loop should be the sa开发者_高级运维me, why the result if so different? Thanks a lot for the answer.
Hm, does temp1.get(j1)
return an Integer
instead of an int
? If so, then the Integer
s in question are not identical (i.e. not ==). Even though they are equal from the point of view of equals()
.
EDIT:
One way to solve this issue is to make temp1 and temp2 return an int
instead of an Integer
. You can use simple autoboxing (or, autounboxing in this case) here.
Say for example that your class is SithLord
and your integer property is jediKills
. Currently your class looks like this:
public class SithLord {
private Integer jediKills;
public Integer getJediKills() { return jediKills; }
}
All you have to do is change it to this:
public class SithLord {
private Integer jediKills;
public int getJediKills() { return (jediKills == null ? 0 : jediKills); }
}
And Java will automatically cast the variable to an int
for you. (Note that I'm assuming here that a null Integer is a zero. That assumption may or may not work for your class. But for the class above it does, as a Sith lord would certainly advertise his kills.)
Alternatively you can do this:
public class SithLord {
private int jediKills;
public int getJediKills() { return jediKills; }
}
Either way the point is that the class needs to return an int
if you want to use the == operator here.
As to why this may have worked in the past for you, I can only speculate since I haven't seen the code. But it's safe to say that == would be true iff the references on either side of the == are one and the same. That can happen if you're exercising that sort of control over the objects. But if you autobox primitive types you give that up, because Java will create new wrappers around the primitive types for you each time you autobox. And because they're new, they won't be the same reference.
Hope that helps.
In java the == operator tests for Object equality (similar to the === operator in PHP or Javascript). If temp.get()
returns an Integer Object rather than an int, and temp2.get(j2)
does not point to the exact same object as temp1.get(j1)
, then the result of temp1.get(j1) == temp1.get(j2)
will be false.
There is also a .equals(Object other)
method on every object in Java that performs semantic equality (the same function as == in PHP or Javascript). So as long as temp2.get(j2)
contains the same number as temp1.get(j1),
temp1.get(j1).equals(temp1.get(j2))` will be true.
The reason the subtraction followed by a comparison to 0 worked is that you set the result into xyz, which is of type int. The java compiler automatically adds the call to the intValue() method of the Integer class when int jxyz = temp1.get(j1) - temp1.get(j2)
is run, which returns the actual int value. In Java, int, float, char, etc. are all considered basic types. For basic types, the == operator compares the value of the basic type.
I realize this is confusing and backwards from the way most other languages handle variable comparisons.
精彩评论