Java 5 introduced auto-boxing which automatically converts int to Integer and because of that many Java developers started writing code for comparing int variable to Integer object and Integer to Integer using == operator. You should avoid that because it can create subtle bugs in your program because it only works for a particular range of integers and not for all. There is another problem as well, when you compare int variable to Integer object using == operator, the Integer object is converted to primitive value. This can throw null pointer exception if the Integer object is null, which can crash your program. Now when you compare two Integer object using == operator, Java doesn't compare them by value, but it does reference comparison. When means even if the two integer has same value, == can return false because they are two different object in heap. That's why you should use equals() method to compare objects. There is one more point which makes thing tricky, caching of integer objects by Integer.valueOf() method. What this mean is that your program will work for some input but not for others. This happens because auto boxing uses Integer.valueOf() method to convert Integer to int and since method caches Integer object for range -128 to 127, it return same Integer object in heap, and that's why == operator return true if you compare two Integer object in the range -128 to 127, but return false afterwards, causing bug. Before Java 1.4 that was not a problem because there was no autoboxing, so you always knew that == will check reference in heap instead of value comparison.
Comparing Integer object with == in Java
Some of the JVM cache objects of some wrapper class e.g. Integer from -128 to 127 and return same object which if compare via “ ==” can return true but after this range this validity doesn't work and to make it worse this behavior is JVM dependent so better avoid this kind of check and use equals() method. e.g.
Integer i1 = 260;
Integer i2 = 260;
if (i1 == i2) {
System.out.println("i1 and i2 is equal");
} else {
System.out.println("i1 and i2 is not equal ");
}
Here you will most probably get "i1 and i2 is not equal " at least in my machine.
because in this case, unboxing operation is not involved. The literal 260 is boxed into two different Integer objects ( again it varies between JVM to JVM) , and then those objects are compared with ==. The result is false, as the two objects are different instances, with different memory addresses. Because both sides of the == expression contain objects, no unboxing occurs.
Integer i1 = 100;
Integer i2 = 100;
if (i1 == i2) {
System.out.println("i1 and i2 is equal");
} else {
System.out.println("i1 and i2 is not equal ");
}
Here, most probably you will get the text "i1 and i2 is equal".
Because int values from -127 to 127 are in a range which most JVM will like to cache so the VM actually uses the same object instance (and therefore memory address) for both i1 and i2. As a result, == returns a true result.
This is very indeterministic and only shown by example since some JVM do optimization at certain integer value and try to return same object every time but its not guaranteed or written behavior.
So best is to avoid this kind of code in post Java 1.5 and instead use equals() method to compare Integers in Java, which is more deterministic and correct.
Nice explanation.
BalasHapusThanks,
https://www.flowerbrackets.com/difference-between-equal-operator-vs-equals-method-java/