Why isn’t there a java.lang.Array class? If a java array is an Object, shouldn’t it extend Object?

From JLS:

Every array has an associated Class object, shared with all other
arrays with the same component type. [This] acts as if: the direct superclass of an array
type is Object [and] every array type implements the interfaces Cloneable
and java.io.Serializable.

This is shown by the following example code:

class Test {
    public static void main(String[] args) {
        int[] ia = new int[3];
        System.out.println(ia.getClass());
        System.out.println(ia.getClass().getSuperclass());
    }
}

which prints:

class [I
class java.lang.Object

where the string "[I" is the run-time type signature for the class object "array with component type int".

And yes, since array types effectively extend Object, you can invoke toString() on arrayObject also see the above example

int arr[] = new arr[2];
arr.toString();

Leave a Comment