Arrays are objects
The
documentation about Arrays says that they are objects that contain a fixed number of values of a particular type.
This can be proved by the following code:
String[] array = new String[10];
System.out.println(array instanceof Object);
The result is:
true
Arrays have multiple personalities
There is a reason for the special syntax in creating arrays. Each array instantiated with a different type dynamically generates a new class.
Suppose you create two arrays below:
String[] array1 = new String[10];
Integer[] array2 = new Integer[10];
If you print the class names of these objects as follows:
System.out.println(array1.getClass().getName());
System.out.println(array2.getClass().getName());
You will see that they return different values, as follows:
[Ljava.lang.String;
[Ljava.lang.Integer;
We can verify that the types are incompatible with each other:
System.out.println(array1.getClass().isAssignableFrom(array2.getClass()));
What we get:
false
Lists are different
As a counterexample, lists that use non-genetic do not exhibit the same behavior.
Consider the lists:
List<String> list1 = new ArrayList<>();
List<Integer> list2 = new ArrayList<>();
If we print the classes, we will see that type erasure was effective because they are identical:
java.util.ArrayList
java.util.ArrayList
And we can verify that they are compatible with each other using:
System.out.println(list1.getClass().isAssignableFrom(list2.getClass()));
From which we get:
true
Builder
Arrays have no constructors in the traditional sense, that is, you can not extend the class and override the constructor.
However, like any object it will have its status initialized at some point by the Virtual Machine.
This can be a disadvantage from the point of view of language flexibility, as it does with primitive types. On the other hand, this allows the JVM to optimize array allocation without relying on Java code.
Storage
Arrays allocate a continuous stretch of memory, whose positions are accessed through the index. Each position contains an element .
Elements contain values directly in the case of primitive types, while objects are stored as references. This causes a new array of primitives to be initialized to the default value of that type, whereas object arrays have their elements initialized with null
.
References vs. Pointers
Content already exists in the OS on this, but basically a reference is something that always points to an object and that allows access to it only through the interface or type of reference used.
You can not point a reference to an arbitrary position in memory or perform mathematical operations on it.