The following code compiles (with Java 8):
Integer i1 = 1000;
int i2 = 1000;
boolean compared = (i1 == i2);
But what does it do?
Un
Lets do some examples :
Case -1 :
public static void main(String[] args) {
Integer i1 = 1000;
int i2 = 1000;
boolean compared = (i1 == i2);
System.out.println(compared);
}
Byte code :
....
16: if_icmpne 23 // comparing 2 integers
....
Case -2 :
public static void main(String[] args) {
Integer i1 = 1000;
Integer i2 = 1000;
//int i2 = 1000;
boolean compared = (i1 == i2);
System.out.println(compared);
}
Bytecode :
...
16: if_acmpne 23 // comparing references
....
So, in case of comparison of Integer and int with == the Integer is unboxed to an int and then comparison happens.
In case of comparing 2 Integers, the references of 2 Integers are compared.