where is array saved in memory in java?

后端 未结 4 2249
半阙折子戏
半阙折子戏 2021-01-04 08:19

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?

相关标签:
4条回答
  • 2021-01-04 09:01

    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 讨论(0)
  • Memory diagram:

    Memory diagram

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

    0 讨论(0)
  • 2021-01-04 09:13

    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.

    0 讨论(0)
  • 2021-01-04 09:16

    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.

    0 讨论(0)
提交回复
热议问题