I am not able to understand the difference in the way obj and obj2 objects are created in the following code. In particular, I am not sure how a primitive is cast to an object. Looking at some of the other questions here, I thought this was not possible. But the following program compiles and runs fine. In the first case, the output is false and in the second case it is true.
public class Test {
public static void main(String args[]){
Integer num = new Integer(3) ;
Object obj = num;
Integer[] integerArr = {1, 2, 3, 4};
Object[] objArr = integerArr;
boolean contains = false;
for (int i = 0; i < objArr.length; i++){
if (objArr[i] == obj){
contains = true;
break;
}
}
System.out.println(contains);
int num2 = 3 ;
Object obj2 = num2;
Integer[] integerArr2 = {1, 2, 3, 4};
Object[] objArr2 = integerArr2;
boolean contains2 = false;
for (int i = 0; i < objArr2.length; i++){
if (objArr2[i] == obj2){
contains2 = true;
break;
}
}
System.out.println(contains2);
}
}