开发者

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?


Memory diagram:

where is array saved in memory in java?

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


In theory, the stack has a single pointer to a location in the heap that contains the array itself. The array itself is just an array of pointers which also point to locations in the heap that contain the objects you reference.

In Java, you can pretty much count on the fact that any time you say new ..., space is being created in the heap. Generally speaking, any time you declare a variable, the compiler will reserve stack space in the method's context for that variable. For native types, that space will hold the actual bytes to represent the value. For objects and arrays, that variable will hold a memory reference.

So, for example, the following objects have separate memory locations allocated for them in the heap:

new Object[20]
new String("abc")
new List() // This contains a reference to an initial array, which is also on the heap.

Note that there are very few times when new String("abc") is preferable to "abc", since string literals are going to exist in the package's memory anyway, and strings are immutable. There's no point allocating extra memory for an exact copy of a string that exists in memory already.

In practice, the only caveat is that the compiler doesn't necessarily have to store local variables on the stack at all. If it determines that the scope of the variable is short enough, it's free to optimize away the stack reference and just use a register for it.


In Java, basically any time you use the new keyword you are allocating space on the heap for an object to be stored.

The variable you use to point to that object contains a reference stored on the stack.

So for example:

                                    // This array object is
                                    // stored on the heap.
String[] arr                      = new String[5];
// This reference (arr) is stored
// in a variable on the stack.

In the case of an array of reference types like an Object[], the space allocated is a contiguous block large enough to store however many references the array will hold. Any particular reference, such as the one at arr[0], will itself point to another location on the heap where the individual object is stored.

The array, somewhere on the heap:
[a*][b*][  ][  ][  ]

a (elsewhere on the heap):
"abc"

b (yet another heap location):
[A List object]

The only exception to this is with arrays of primitives, like int[]: in this case the array itself is still a contiguous block on the heap, but each position within the array contains the actual value itself rather than a reference to another position on the heap.


An Array of n Strings is

  • A subsequent list of n object references on the heap
  • n single String objects with a reference to an array on the heap
  • n arrays of chars on the heap

This Array then has a reference as well that can be held on the stack or (as a field on a class) in the heap

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜