[Java Small note] Understanding of array memory management

Source: Internet
Author: User

Arrays are a composite structure that is provided by most programming languages, and if a program requires multiple types of variables of the same type, you can consider defining an array of Java-language array variables when referencing a variable of type, and therefore having the attributes of a Java reference variable. Arrays must be initialized before using the array, and when all array elements are allocated the appropriate memory space and the initial value is specified, the array initialization is complete.

Array initialization consists of

1. Static initialization: When initialized, the initial value of each array element is explicitly specified by the programmer, and the length of the array is determined by the system.

Grammatical form:

Int[] y = {1,2,3,4,5};

2, dynamic initialization: The programmer only specifies the length of the array when initializing, and the system assigns the initial value to the program.

Grammatical form:

int[] x = new INT[5];

Here's a look at static initialization arrays and dynamic initialization of arrays in memory distribution

  

1 //Code2  Public Static voidMain (string[] args) {3     4         //initializes the first array with static initialization5string[] Jops =Newstring[]{"Workers", "peasants", "officials", "singers"};6         //Initializes a second array with static initialization7string[] names =Newstring[]{"Xiao Zhang", "Xiao Ming", "Xiao Gang"};8         //initializes the first array with dynamic initialization9string[] str =NewString[5];TenSystem.out.println ("The length of the first array:" +jops.length); OneSystem.out.println ("The length of the second array:" +names.length); ASystem.out.println ("The length of the third array:" +str.length); -}

The length of the three arrays is 4,3,5 in sequence.

Here is the memory schematic:

The Java array is dynamic, and once the array is initialized, the memory space allocation of the array elements ends, and the program can only change the values of the array elements, not the length of the array. An array variable of Java is a variable of a reference type, and the array variable is not the array itself, it simply points to the data array object in the heap memory. We can see from the following code that the array object changes in the memory of the relationship.

1 jops = names; 2         str = names; 3         System.out.println ("The length of the first array:" +jops.length); 4         System.out.println ("The length of the third array:" +str.length); 5         Jops[1] = "Xiao Li"; 6         System.out.println ("The second element of the names array:" +jops[1]);
View Code

Memory changes in heap memory:

As we can see, the Java array variable is simply a variable of the reference type, it is not the array object itself, so long as the array variable always points to a valid array object, the array variable can be used in the program.

[Java Small note] Understanding of array memory management

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.