The expressions used in the following Java code are nuts and pretty much unacceptable though I tested them just for a general purpose and obtained some unexpected result. The simple code snippet goes following.
package wrapper;
final public class Main
{
public static void main(String[] args)
{
Integer j1 = 127;
Integer j2 = 127;
System.out.println(j1==j2); //returns true!!!
Integer k1 = 128;
Integer k2 = 128;
System.out.println(k1==k2); //returns false!!!
Integer w1 = -128;
Integer w2 = -128;
System.out.println(w1==w2); //returns true!!!
Integer m1 = -129;
Integer m2 = -129;
System.out.println(m1==m2); //returns false!!!
}
}
Integer j1 = 127;
Integer j2 = 127;
System.out.println(j1==j2);
The above code obviously displays true hence, there is no question about it.
Integer k1 = 128;
Integer k2 = 128;
System.out.println(k1==k2);
The above code is expected to display true on the console but surprisingly, it shows false. Why?
Integer w1 = -128;
Integer w2 = -128;
System.out.println(w1==w2);
This code displays true and no question about it.
Integer m1 = -129;
Integer m2 = -129;
System.out.println(m1==m2);
The above code again displays false though it is expected to return true. Why?
Integers from the range -128..127 are cached, so j1 and j2 from:
Integer j1 = 127;
Integer j2 = 127;
point to the same object.
Of course, this is a JVM implementation thing, so you should never assume they are cached. For comparing reference types, never use == but use their equals(...) method instead.
Note that there's a difference between
int k1 = 128;
int k2 = 128;
System.out.println(k1==k2);
and
Integer k1 = 128;
Integer k2 = 128;
System.out.println(k1==k2);
The first one is creating simple numeric types. The second on is creating Object types which wrap up the numeric types to allow them to be passed to functions which expect Objects.
In the first case, simple numeric types only have a value and this is compared when == is used. However, the Objects which are created don't use == to compare their values. Rather they use == to say if they are the same object and a .equals() method to say if they have the same value. There is no operator overloading in Java, so any time objects are compared with ==, it's always asking the question "is this the same object?" rather than "do these two objects have the same value?"
So, in this case, the code you gave winds up being equivalent to
Integer k1 = new Integer(128);
Integer k2 = new Integer(128);
System.out.println(k1==k2);
This returns false because k1 and k2 are not the same object. If instead you do:
Integer k1 = 128;
Integer k2 = k1;
System.out.println(k1==k2);
You'll get true.
The surprising part is actually that any of the results from your tests above are true. This is caused by the fact that Java keeps a set of Integers for small numbers (-128 to 127, i.e. one byte size) and uses those for assignments when it can.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With