[Simple Java] how to store Java arrays in memory, simplejava

Source: Internet
Author: User

[Simple Java] how to store Java arrays in memory, simplejava

Java has two types of Arrays:

  • Array of basic data types;
  • Object array;

When an object is created with the keyword "new", the memory space is allocated on the stack and the reference of the object is returned. This is the same for arrays, because arrays are also an object;

One-dimensional array
int[] arr = new int[3];

In the above Code, the arr variable stores the reference of the array object. If you create an integer array with a space of 10, the situation is the same, the space occupied by an array object is allocated on the stack and Its Reference is returned;

Two-dimensional array

How are two-dimensional arrays stored? In fact, there is only one-dimensional array in Java, and the two-dimensional array is an array that stores the array, the following code and:

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

For multi-dimensional arrays, the truth is the same;

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

In Java, arrays are also an object, so how to store objects in memory is also applicable to arrays;

As we all know, the java runtime data zone includes the heap, JVM stack, and others. The following code is a small example. Let's take a look at how the array and Its Reference are stored in the memory.

class A {    int x;    int y;}...public void m1() {    int i = 0;    m2();}public void m2() {     A a = new A();}...

With the code above, let's call method m1 to see what happened:

When m1 is called, the stack Frame-1 is created and pushed to the stack, and the local variable I is also created within the stack Frame-1.

Then, the m2 method is called inside the m1 method. The stack Frame-2 is created and pushed to the stack. In the m2 method, A new object A is created in the heap, its reference is put into Frame-2 of the stack Frame;

The general situation of heap and stack in the memory is as follows:

Arrays are also objects, so the distribution of arrays and objects and references in memory is shown above;

 

Http://www.programcreek.com/2013/04/what-does-a-java-array-look-like-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.