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?