In Java, does an array definition allocate space for references or for objects themselves?

In the next line of code, does the compiler allocate memory to store 10 MyClass objects or 10 links?

MyClass[] arr= new MyClass[10]; 

In other words, do arrays only store references or the objects themselves?

Also, is the behavior different from primitive types?

+4
source share
2 answers

It allocates space for links. In the case of primitive types, it allocates the size of the byte of space = an array of length * of the primitive type.

+5
source

When creating this type, arrays are automatically initialized with a default value for their type, so arr initialized with 10 null references.

+5
source

All Articles