I used this code. I am confused why this int array is not converted to an object vararg argument:
class MyClass {
static void print(Object... obj) {
System.out.println("Object…: " + obj[0]);
}
public static void main(String[] args) {
int[] array = new int[] {9, 1, 1};
print(array);
System.out.println(array instanceof Object);
}
}
I expected the output:
Object…: 9
true
but it gives:
Object…: [I@140e19d
true
Object...
meansObject[]
andint[]
can not be converted toObject[]
(int
is a primitive, not anObject
). However, theint[]
array itself can be interpreted asObject
. So you end up passing an object arrayObject[]
with exactly one element in it, anint[]
. So you have an array of arrays.