Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are Java arrays class instances?

Tags:

java

arrays

oop

jls

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?

like image 755
Aldán Creo Avatar asked Jan 16 '21 12:01

Aldán Creo


People also ask

Does Java array is an instance of object?

In the Java programming language, arrays are objects (§4.3. 1), are dynamically created, and may be assigned to variables of type Object (§4.3. 2). All methods of class Object may be invoked on an array.

Is an array an instance variable in Java?

An array is an instance of a special Java array class and has a corresponding type in the type system. This means that to use an array, as with any other object, we first declare a variable of the appropriate type and then use the new operator to create an instance of it.

Are arrays classes in Java?

The Arrays class in java. util package is a part of the Java Collection Framework. This class provides static methods to dynamically create and access Java arrays. It consists of only static methods and the methods of Object class.

What is arrays class in Java?

Arrays class in Java. The Arrays class in java.util package is a part of the Java Collection Framework. This class provides static methods to dynamically create and access Java arrays. It consists of only static methods and the methods of Object class.

What is the difference between class and instance in Java?

Instances in Java are known as Objects. An object is a real-life entity, whereas a Class is a group of similar objects. An object is created from the class. Dog is a class that is a real-life entity. Basically, instance and object are the same thing. We create an instance of the Dog class using the new keyword.

What is the use of this class in Java?

This class provides static methods to dynamically create and access Java arrays. It consists of only static methods and the methods of Object class. The methods of this class can be used by the class name itself. The class hierarchy is as follows:

What are instance methods in Java?

Instance Methods are the group of codes that performs a particular task. Sometimes the program grows in size, and we want to separate the logic of the main method from other methods. A method is a function written inside the class. Since java is an object-oriented programming language, we need to write a method inside some classes.


1 Answers

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
like image 143
Thomas Fritsch Avatar answered Sep 28 '22 00:09

Thomas Fritsch