12

I'm trying to understand the array setup in java. Why must you initialize space for each each object in the array, after you have created the array. How is it stored in memory like this:

[object][object]

or like this:

[*class]->[object]  
[*class]->[object]

In other words, what is actually being done in memory. Does array[0] = new class() just return a reference to a reserved location in memory, and the class[] array = new class[10] statement create something along the lines of 10 pointers, which are later assigned to by the new statements?

Ghasem
  • 14,455
  • 21
  • 138
  • 171
rubixibuc
  • 7,111
  • 18
  • 59
  • 98
  • possible duplicate of [Java Array is stored in stack or heap?](http://stackoverflow.com/questions/2099695/java-array-is-stored-in-stack-or-heap) – Thomas Aug 31 '15 at 20:08

3 Answers3

19

Arrays in Java store one of two things: either primitive values (int, char, ...) or references (a.k.a pointers).

So, new Integer[10] creates space for 10 Integer references only. It does not create 10 Integer objects (or even free space for 10 Integer objects).

Incidentally that's exactly the same way that fields, variables and method/constructor parameters work: they too only store primitive values or references.

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
  • 1
    Is there a difference in memory between the way arrays of primitives are stored than arrays of objects – rubixibuc Apr 06 '11 at 10:05
  • 3
    Since there are no references to primitive values, the values themselves are stored in the array instead of references. – Joachim Sauer Apr 06 '11 at 10:40
  • 1
    What about the length? Arrays in Java know their length. Isn't that stored somewhere? Or is that a compile-time-only thing? – GlenPeterson Sep 17 '16 at 22:38
7

If you are familiar with C/C++ you can think of Java object references as pointers to objects (or pointers to structs). So:

Person p = new Person();
p.setName("Helios");

is:

  • declare a p pointer to a Person struct (in the stack)
  • reserve memory for and initialize Person struct
  • assign its address to p
  • execute method setName on object referenced by p

So when you are doing:

Person[] ps = new Person[5];

you are reserving an array of 5 references to Person. Next you will have to create each real person and assign each reference to a place in the array.

Edit: the (almost) C/C++ version of the previous code

class Person { ... };
typedef PersonStruct* Person; // I don't remember if this declaration is ok
Person p = new PersonStruct();
p -> setName(...);

Person[] ps = new Person[5]; 
// ps is a variable in the stack pointing to the array in the heap
// (being the array five references to the PersoStruct)

and you could do

ps[3] = p;
helios
  • 13,574
  • 2
  • 45
  • 55
  • So it is almost as if you are creating a person *p[] array then assigning one by one to p[0] throughout p[length-1]? – rubixibuc Apr 06 '11 at 10:08
  • Yes. It's like this. In my example code ps is the array. It's also a reference (located in the stack) to the array structure (located in the heap). The array structure is the one who will hold the references to the five Person objects. I'll add a C/C++ translation :) – helios Apr 07 '11 at 09:21
0

Arrays are continuous space of memory, so they look like more your first sketch:

[object-reference][object-reference]

array[0] = new class() will store in array[0] a reference to the new created object.

class[] array = new class[10] will create an array of ten empty slots (or ten null references).

Manuel Salvadores
  • 16,287
  • 5
  • 37
  • 56