where is array saved in memory in java?

Greg Oks picture Greg Oks · Aug 10, 2011 · Viewed 10.6k times · Source

If I have a function that in that function I declare:

Object arr[] = new Object[20];

Where are arr and the whole array stored? heap? stack? Does it matter if the declaration is in some function or in main()?

and let's say I also have these command lines:

arr[0] = new String("abc");
arr[1] = new List();

where are arr[0] and arr[1] stored?

Answer

Nayuki picture Nayuki · Aug 10, 2011

Memory diagram:

Memory diagram

Boxes are memory locations (where binary numbers can be stored).
Arrows are memory references (i.e. pointers).