Array and its memory control, Array Memory Control

Source: Internet
Author: User

Array and its memory control, Array Memory Control

Author: Zen building wangyue (http://www.cnblogs.com/yaoyinglong/) Static Java Array

Java is a typical static language. Therefore, arrays are also static. That is, after the array is initialized, the length of the array is unchangeable.

In java, array variables are reference types. What does that mean? This means that the array variable is not an array, but an array object in the heap memory. For example:

The three variables and their respective referenced arrays are allocated in the memory.

We can change the direction of the array variable to make it point to other array objects in the heap memory (provided that their compilation type is compatible ), on the surface, it seems that the length of the array is changed. But in fact, we just let the array variable point to another array object, and the original array object will not point to other array variables, and then wait for garbage collection.

For an array variable, it does not need to be initialized, but just points the array variable to a valid array object.

For Java programs, all referenced variables do not need to be initialized. Only the objects referenced by the referenced variables need to be initialized.

All partsVariableThey are all stored in stack memory, regardless of the reference type.VariableOr basic typeVariable, Are stored in their respective method stack; but the referenced Type VariableObject(Including arrays and common Java objects) are always stored in the heap memory.

For Java, objects in the heap memory (whether an array or a common Java object) are not allowed to be accessed directly. To access objects in the heap memory, variables can only be referenced.

Array variables are stored in the stack memory, but the array elements are saved in the heap memory as part of the array objects, whether they are basic array elements or referenced array elements. Initialization of basic type Arrays

For an array of basic types, the values of array elements are directly stored in the corresponding array elements, so the program directly allocates memory space for the array, save the value of the array element to the corresponding memory.

IntArr is just an array variable. It exists in the method stack area where it is located and actually points to an array object in the memory. Therefore, the array variables should be separated from the array objects. Initialization of an array of reference types

It is a little more complicated than the initialization of the basic type array: the elements in the referenced type array are stored or referenced, rather than the object itself, it points to another memory unit in the heap memory (this memory stores the objects pointed to by reference variables: arrays or Java objects ).

Public class Person {private String name; private int age; public Person (String name, int age) {this. name = name; this. age = age;} public String getName () {return name;} public void setName (String name) {this. name = name;} public int getAge () {return age;} public void setAge (int age) {this. age = age;} public String toString () {return "my name is" + name + "My age is:" + age;} public static void main (String [] Args) {Person [] students = new Person [3]; Person xiaoming = new Person ("James", 10); Person xiaohong = new Person ("xiaohong", 15 ); person xiaozhang = new Person ("Xiao Zhang", 20); students [0] = xiaoming; students [1] = xiaohong; students [2] = xiaozhang; System. out. println (Arrays. toString (students); System. out. println ("Does xiaoming and students [0] point to the same Java object? --> "+ (Xiaoming = students [0]); xiaohong. setName ("My name changed"); System. out. println ("modified the name attribute of the Java object to which it points through the variable xiaoming:" + xiaohong. getName (); System. out. print ("the name attribute for obtaining students [1] is:"); System. out. println (students [1]. getName ());}}
The output is as follows:
 

Memory usage during main function execution:

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.