Understanding the internal mechanism of a Java array

Source: Internet
Author: User

We know that an array is a reference data type, an array reference variable is just a reference, array elements and arrays of variables are stored separately in memory, and the following describes how the array operates in memory.

1. In-memory arrays

An array reference variable is simply a reference, and this reference variable can point to any valid memory, and the array element can be accessed through the array variable only if it points to valid memory, because the reference variable is the fundamental way to access the real object. That is, if we want to access the array in the program, it can only be accessed through the reference variable of the array. While the actual array elements are stored in heap memory, the reference variables are stored in stack memory.

When a method executes, each method builds its own memory stack, and the variables defined in the method are stored in the stack memory, and when the method executes, the memory stack of the method is naturally destroyed. So the variables defined in all the methods are stored in the memory stack, and when we create an object in memory, the object is saved in the runtime data area, so that it can be reused because the object is typically created at a large cost, which is the heap memory. The object in memory is not destroyed with the end of the method, and the garbage collection mechanism of the system will reclaim it at the appropriate time only if an object does not have any reference variables to reference it.

If the array in the heap memory no longer has any variable references pointing to itself, then the array becomes garbage, and the memory it occupies will be reclaimed by the garbage collection mechanism of the system.

One of the things to note here is that in Java, as long as the types are compatible, you can point an array variable to another actual array, and many people here think that the length of the Java array is variable, but it is just the illusion that this mechanism makes people.

So when we look at arrays, we should think of arrays as two parts: one is an array reference, that is, a reference variable defined in code, the other is the actual array itself, and this part is in system memory, which we usually cannot access directly, only through the array reference variable.

2. Initialization of array elements

such as the following code:

public class shuzu{

public static void Main (string[] args) {

Int[] Iarr;

Iarr = new Int[5];

for (int i=0;i<iarr.length;i++) {

Iarr[i] = i + 10;

}

}

}

In the above program, first defines an array reference variable of type int, when executing int[] iarr;  , only a null reference is defined in the stack memory, and the reference does not point to any valid memory, so it is not possible to specify the length of the array when executing "Iarr = new int[5];" When dynamic initialization is implemented, the system is responsible for allocating memory space for the array, assigning default initial values, and all array elements are assigned by default to 0.

Understanding the internal mechanism of a Java array

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.