Aldan Creo
Aldan Creo

Reputation: 854

Are Java arrays class instances?

The Java Language Specification says:

An object is a class instance or an array.

And it also says:

arrays [...] may be assigned to variables of type Object

But the part that confuses me is:

The class Object is a superclass of all other classes

If arrays may be assigned to variables of type Object, then it must mean that arrays can be Objects (not only behave as, but to be instead). Then it means that an array is a class instance, which does not seem to be consistent with the first quote (if it were, then why would it be listed as a different thing?).

How can all this fit together?

Upvotes: 8

Views: 2488

Answers (5)

Aldan Creo
Aldan Creo

Reputation: 854

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!]

Upvotes: 3

Thomas Fritsch
Thomas Fritsch

Reputation: 10127

There is no contradiction. An array is also an Object, albeit a special kind of Object.
It is like saying: An bird is also an animal, albeit a special kind of animal.

You can convince yourself by compiling and running the following Java code.

    String[] arrayOfStrings = { "bla", "blah" };
    
    // examine the class hierarchy of the array 
    System.out.println("arrayOfStrings is of type "
            + arrayOfStrings.getClass().getSimpleName()
            + " which extends from "
            + arrayOfStrings.getClass().getSuperclass().getSimpleName());
    
    // assingning the array to a variable of type Object
    Object object = arrayOfStrings;

The output will be

arrayOfStrings is of type String[] which extends from Object

Upvotes: 7

Vasily Liaskovsky
Vasily Liaskovsky

Reputation: 2528

Arrays are special classes provided to you by Java itself. All of them inherit from common superclass Object. As they inherit from Object they of course can be used anywhere where Object is expected. Instances of arrays are indeed instances of those classes. One can even reference array classes as they do with other classes' literals:

    Class<int[]> intArrayClass = int[].class;

I see no conflict.

This can be useful https://docs.oracle.com/javase/specs/jls/se7/html/jls-10.html#jls-10.8

Upvotes: 6

Gerold Broser
Gerold Broser

Reputation: 14762

Your second link also says:

All methods of class Object may be invoked on an array.

So from a dev's POV, at least, it's an Object though there is no java.lang.Array (that is exposed to us) it has been instanced from.

A second indication is that arrays are also stored on the heap.

Upvotes: 0

user14410324
user14410324

Reputation:

The class Object is a superclass of all other classes

All classes in Java extend Object. The class name is actually Object (proper name capitalized). It's why all classes have the method toString() and hashCode(), because they all inherited it from Object.class

An object is a class instance or an array.

An object (lowercase, not a proper name) is the instance generated by the new keyword. ie: File is a class and when you call new File() you just made a File object. I honestly think they should have called it a class instance. (clarification: I wish they never called an instance an object)

arrays [...] may be assigned to variables of type Object

Object[] is an array that can contain instances of type Object.

Object[] obj = new Object[100];

obj instanceof Object evaluates to true.

Upvotes: 0

Related Questions