Arrays are Objects and all the objects come from a class. If I execute the following code:
public class Test {
public static void main(String[] args) {
String str = "Hello";
System.out.println(str.getClass());
}
}
The output is class java.lang.String
.
But if I execute the following:
public class Test {
public static void main(String[] args) {
int arr[] = new int[10];
System.out.println(arr.getClass());
}
}
The output is class [I
.
My questions are:
- What is the class of the arrays?
- Why is it the result?
- If I would like to use the
instanceof
operator how should I use it? If I executeSystem.out.println(arr instanceof Object);
, it works perfectly.