Why Isn't There a Java.Lang.Array Class? If a Java Array Is an Object, Shouldn't It Extend Object

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();

Are Java arrays class instances?

Yes... and no. It is true, indeed, that new Object[100] instanceof Objecttrue, but this is missing the true nature of arrays in Java. Arrays are objects (lowercase), but not Objects (capitalized). Being objects, for example, you have to use the new operator to allocate space for them.

However, the Java Language Specification is right to say that "An object is a class instance or an array", because arrays are fundamentally different from a regular Object. They are inherited from languages such as C++, that were much more deeply rooted in the low-level architecture of computers.

"arrays [...] may be assigned to variables of type Object" only because Java provides an interface for us, programmers, to refer to arrays as Objects. In fact, the JLS says:

All methods of class Object may be invoked on an array

Which of course is true, but it does not logically imply they are Objects. Arrays are not true Objects; therefore, they are not true class instances, and therefore the sentence "The class Object is a superclass of all other classes" doesn't apply here.

All in all, Java is not a pure Object-Oriented programming language (primitives are not objects, for example, but they are nonetheless present in Java). And arrays are a language feature that Java includes that behave as if they were class instances of the Object class, but are not actually class instances of it.

[This is my attempt at summarising the main points made here. Thanks a lot to all of you for your ideas, and feel free to add more!]

Why are the 'Arrays' class' methods all static in Java?

In Java there is no way to extend the functionally of an array. Arrays all inherit from Object but this gives very little. IMHO This is a deficiency of Java.

Instead, to add functionality for arrays, static utility methods are added to classes like Array and Arrays. These methods are static as they are not instance methods.

In Java, what are the semantics of an array as an object?

Extract from Bozho answer to a similar question
Why isn't there a java.lang.Array class? If a java array is an Object, shouldn't it extend Object?

Arrays are a language feature - they have a specific syntax for
declaring and accessing. And their class definition is hidden from
you.

They have a representation in the refleciton API -
java.lang.reflect.Array

Java Array objects returns array's type instead of Array

The class of what the reference is pointing to is a String array. if you want to test to see if that reference is part of an array, then there is a special method for that, isArray.

System.out.println(stt.getClass().isArray());

As some other have noted, with a closer look you can see the difference in the output just using

class [Ljava.lang.String; // Array of String
class java.lang.String // String

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();

Java : Super class array object assigned with sub class array object

You can't do this:

Cclass[] child = new Cclass[10];
Pclass[] parent = child;
parent[0]=new Pclass();

You should try doing this:

Cclass[] child = new Cclass[10];
Pclass[] parent = child;
parent[0]=new Cclass();

That's because, You first assigned the Pclass array to the child reference that can only have Cclass objects, then you are trying to assign Pclass object to the parent reference, that's not allowed!

See, what happens is that you have created a Cclass object on the heap when you wrote new Cclass, though the Cclass objects were null in the array but now they would accept only Cclass objects or it's subclass's objects

so assigning the Pclass object would be illegal!

Reason for getting a runtime exception and not compile time:

The compiler only checks whether the classes are in the same inheritance hierarchy or not, since they are in the same hierarchy you get a Runtime exception.

Java Array objects returns array's type instead of Array

The class of what the reference is pointing to is a String array. if you want to test to see if that reference is part of an array, then there is a special method for that, isArray.

System.out.println(stt.getClass().isArray());

As some other have noted, with a closer look you can see the difference in the output just using

class [Ljava.lang.String; // Array of String
class java.lang.String // String


Related Topics



Leave a reply



Submit