How does creating an Array work?

Asked

Viewed 590 times

1

When using the command new() for the creation of any object we call the constructor of the class of that object and obtain the reference to the instance created. However, what happens when creating an Array?

Where does it point (I know that this term is inconsistent with Java, because we are not working with pointers) the reference returned in creation? I understand that it is not for an object of the type, because we are not using the constructor of the class.

  • Points to the same place when you do int a = 6; String a = "";

2 answers

3


Arrays are objects

To documentation about Arrays says that they are objects that contain a fixed number of values of a certain 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 instantiated array with a different type generates a new class dynamically.

Suppose the creation of two arrays below:

String[] array1 = new String[10];
Integer[] array2 = new Integer[10];

If you print the name of the classes of these objects like this:

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

From what we get:

false

Lists are different

As a counterexample, lists using genetics nay have the same behaviour.

Consider the lists:

List<String> list1 = new ArrayList<>();
List<Integer> list2 = new ArrayList<>();

If we print the classes, we see that the 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 do not have constructors in the traditional sense, that is, you cannot extend the class and override the constructor.

However, as every object it will have its state initialized at some point by the Virtual Machine.

This can be a disadvantage from the point of view of language flexibility, just as it occurs 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 chunk of memory, whose positions are accessed through the index. Each position contains a element.

The elements directly contain the values in the case of primitive types, while objects are stored as references. This causes elements to be initialized with the default value of that type in a new array of primitives, while object arrays have their elements initialized with null.

References vs. Pointers

There is already content here in the OS about this, but basically a reference is something that always points to an object and allows access to it only through the interface or type of reference used.

You cannot point a reference to an arbitrary position in memory or perform mathematical operations on them.

2

You’re using the constructor yes. It’s something internal, the syntax is a bit, but at the bottom you’re using a constructor.

int[] a = new int[10];

If there were no proper syntax in the language, it would be written like this:

Array<int> a = new Array<int>(10);

Or

int[] a = {1, 2 , 3};

Seria:

int[] a = new Array<int>(3);
a[0] = 1;
a[1] = 2;
a[2] = 3;

I put in the Github for future reference.

I do not know if I understood about the pointer, but there is no difference from the rest. On the contrary, often objects end up using a array internal to its constitution.

The creation of array is equal to any object, memory is allocated in the heap and eventually data is stored there, otherwise it is initialized with a default value (zero according to type). The allocated address will be the reference stored in the variable.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.