How does creating an Array work?

1

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

Where do you point to (I know 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 type, as we are not using the class constructor.

    
asked by anonymous 14.03.2016 / 13:11

2 answers

2

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.

    
15.03.2016 / 04:40
1

You are using the constructor yes. It's something internal, the syntax is a bit, but in the background is using a constructor.

int[] a = new int[10];

If there were no syntax of your own in the language, it would look like this:

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

Or

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

It would be:

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

I do not know if I understand the pointer, but there is no difference from the rest. On the contrary, objects often end up using an internal array for their constitution.

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

    
14.03.2016 / 13:30