How the Java array is stored in memory

Source: Internet
Author: User

Read Catalogue
    • One-dimensional arrays
    • Two-dimensional arrays
    • Where are the array objects and their references stored in memory?

There are two types of arrays in Java:

    • An array of basic data types;
    • An array of objects;

When an object is created with the keyword "new", the memory space is allocated on the heap, and then the reference to the object is returned, which is the same for the array, because the arrays are also an object;

One-dimensional arrays
int[] arr = new INT[3];

In the above code, the ARR variable holds a reference to the array object, and if you create an array of shapes with a spatial size of 10, the same is true when the space occupied by an array object is allocated on the heap and then returned to its reference;

Two-dimensional arrays

So how are two-dimensional arrays stored? In fact, there is only one-dimensional array in Java, and a two-dimensional array is an array of arrays that contain the following code:

int[[] arr = new int[3][];arr[0] = new Int[3];arr[1] = new Int[5];arr[2] = new INT[4];

For multidimensional arrays, the rationale is the same;

Where are the array objects and their references stored in memory?

In Java, an array is also an object, so how the object is stored in memory applies equally to arrays;

As we all know, the Java Runtime Data area includes heaps, JVM stacks, and others. The following code is a small example, so let's take a look at how the array and its references are stored in memory.

Class A {    int x;    int y;} ... public void M1 () {    int i = 0;    M2 ();} public void m2 () {     A A = new A ();} ...

As on the code, let's call the method M1 and see what happens:

When M1 is called, the stack frame Frame-1 is created and pushed to the stack, and the local variable i is also created within the stack frame Frame-1.

Then, the M2 method is called inside the M1 method, the stack frame Frame-2 is created and pushed to the stack, and in the M2 method a new object A is created in the heap, and its reference is put into the stack frame Frame-2;

In-memory heap and stacks are now in the general situation such as:

Arrays are also objects, so arrays and objects as well as references in memory are distributed as shown above;

Link: http://www.programcreek.com/2013/04/what-does-a-java-array-look-like-in-memory/

How the Java array is stored in memory

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.