Note point equals () methods and ==

My first reduced before the roll-over accident scene of the accident.

 

 

 The above is what I traverse two sets, and then compare whether the parent ID county and city ID equal, then the county information into the collection belongs to the city, when traversed Over ninety City, noticed distintVo.getPid () == cityVo.getId () will always be false, and I was just up to be crazy, obviously watching values ​​are equal but return is false.

At that time I almost shed a tear unskilled ~~ (ha ha ha, joke, tears started to flow over a child)

Comparison of the two objects are of type Integer, Integer type == comparison with the latter method of inspection time when the value of the time will be more than 127 returns false, do the following Test I:

int i1 = 128;
Integer i2 = 128;
Integer i3 = new Integer(128);

Integer i4 = 127;
Integer i5 = 127;
Integer i6 = 128;
Integer i7 = 128;

Integer i8 = new Integer(127);
Integer i9 = new Integer(127);

i1 == i2 return true; i1 == i3 return true; i4 == i5 returns true, i6 == i7 return false; i8 == i9 return false; return i8.equals (i9) true; i4 == i8 returns false .

Summary: == comparison is whether two objects are the same, they point to look at the first address are equal; the equals () compare logic value is two objects are equal;

Is a wrapper class Integer int, int initial value is 0, integer initial value is null; anyway new new Integer and Integer () does not like, which is not the same memory address, the use of comparisons are == false;

Two out are non new Integer, use == comparison, returns true if the value is between -128 to 127, returns or false;

Are out of the two new Integer, comparisons are used == returns false, the comparison value is equal to required equalse () comparison method;

int and Integer (regardless of whether new) comparison, returns are true, because Integer will be auto-unboxing to int to compare.

 

Guess you like

Origin www.cnblogs.com/afeng-chen/p/11926485.html