Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

where is array saved in memory in java?

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?

like image 239
Greg Oks Avatar asked Aug 10 '11 18:08

Greg Oks


1 Answers

Memory diagram:

Memory diagram

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

like image 181
Nayuki Avatar answered Oct 13 '22 17:10

Nayuki