Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are arrays Objects, but can not be used as a base class?

The Java language specification specifies that

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.

So, considering arrays are objects — why did the Java designers make the decision not to allow inherit and override from it, for example, toString() or equals()?

The current syntax wouldn't allow creating anonymous classes with an array as the base class, but I don't think that was the reason for their decision.

like image 809
Heisenberg Avatar asked Dec 16 '14 12:12

Heisenberg


People also ask

Can arrays be any base type?

The base type of an array can be any legal Java type. From the primitive type int, the array type int[] is derived. Each element in an array of type int[] is a variable of type int, which holds a value of type int.

Why do we use array of objects?

Advantages of Array of Objects: The array of objects represent storing multiple objects in a single name. In an array of objects, the data can be accessed randomly by using the index number. Reduce the time and memory by storing the data in a single variable.

Can you have an array of class objects?

Array of Objects in c++ Like array of other user-defined data types, an array of type class can also be created. The array of type class contains the objects of the class as its individual elements. Thus, an array of a class type is also known as an array of objects.

What is array class and object in Java?

We know that an array is a collection of the same data type that dynamically creates objects and can have elements of primitive types. Java allows us to store objects in an array. In Java, the class is also a user-defined data type. An array that conations class type elements are known as an array of objects.


2 Answers

Java was a compromise between non-object languages and very slow languages of that time where everything was an object (think about Smalltalk).

Even in more recent languages, having a fast structure at the language level for arrays (and usually maps) is considered a strategic goal. Most people wouldn't like the weight of an inheritable object for arrays, and certainly nobody wanted this before JVM advances like JIT.

That's why arrays, while being objects, weren't designed as class instances ("An object is a class instance or an array"). There would be little benefit in having the ability to override a method on an array, and certainly not a great-enough one to counterbalance the need to check for the right method to apply (and in my opinion not a great-one enough to counterbalance the increased difficulty in code reading, similar to what happens when you override operators).

like image 61
Denys Séguret Avatar answered Sep 26 '22 01:09

Denys Séguret


I came across UNDER THE HOOD - Objects and arrays which explains almost anything you need to know about how JVM handles arrays. In JVM, arrays are handled with special bytecodes, not like other objects we are familiar with.

In the JVM instruction set, all objects are instantiated and accessed with the same set of opcodes, except for arrays. In Java, arrays are full-fledged objects, and, like any other object in a Java program, are created dynamically. Array references can be used anywhere a reference to type Object is called for, and any method of Object can be invoked on an array. Yet, in the Java virtual machine, arrays are handled with special bytecodes.

As with any other object, arrays cannot be declared as local variables; only array references can. Array objects themselves always contain either an array of primitive types or an array of object references. If you declare an array of objects, you get an array of object references. The objects themselves must be explicitly created with new and assigned to the elements of the array.

Arrays are dynamically created objects, and they serve as a container that hold a (constant) number of objects of the same type. It looks like arrays are not like any other object, and that's why they are treated differently.

like image 36
Maroun Avatar answered Sep 24 '22 01:09

Maroun