Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Java, defining an array allocates space for references or for the objects themselves?

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

MyClass[] arr= new MyClass[10];

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

Also, is the behaviour different for primitive types?

like image 551
Shailesh Tainwala Avatar asked Dec 27 '22 19:12

Shailesh Tainwala


1 Answers

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

like image 92
Marcelo Avatar answered Feb 05 '23 10:02

Marcelo