Initialization of an array of reference types

Source: Internet
Author: User

The array element of the reference type array is referenced, so the situation becomes more complicated: the storage or reference in each array element points to another memory, which stores valid data.

To better illustrate the running process of the reference type array, the following defines a Person class (all classes are reference types ), for more information about defining classes, objects, and references, see Chapter 5th. The Person class code is as follows:

Program list: codes/04/4-6/Person. javapublic class Person {// age public int age; // height public double height; // defines an info method public void info () {System. out. println ("My age is:" + age + ", my height is:" + height );}}

The following program defines an array of persons [], then dynamically initializes the array of persons [], and specifies a value for each array element of this array. The program code is as follows:

Program list: codes/04/4-6/TestReferenceArray. javapublic class TestReferenceArray {public static void main (String [] args) {// defines a students array variable, whose type is Person [] Person [] students; // execute the dynamic initialization students = new Person [2]; // create a Person instance and assign this Person instance to the Person zhang variable Person zhang = new Person (); // assign a value to the property of the Person object referenced by zhang. age = 15; zhang. height = 158; // create a Person instance and assign this Person instance to the lee variable Person lee = new Person (); // assign the property of the Person object referenced by lee Value: lee. age = 16; lee. height = 161; // assign the value of the zhang variable to the first array element students [0] = zhang; // assign the value of the lee variable to the second array element students [1] = lee; // the results of the following two lines of code are exactly the same, because lee and students [1] point to the same Person instance. Lee.info (); students [1]. info ();}}

The execution process of the above Code represents a typical initialization process of the reference type array. The following describes the execution process of this Code in detail.

When executing the Person [] students; Code, this line of code only defines a reference variable in the stack memory, that is, a pointer, which does not point to any valid memory zone. The memory storage diagram is shown in:

The stack memory in defines a students variable, which is just a reference and does not point to any valid memory. Until Initialization is executed, this program performs dynamic Initialization on the students array. The system allocates the default initial value: null for the array elements, that is, the value of each array element is null, the storage diagram after dynamic Initialization is shown in:

It can be seen that both array elements of the students array are references, and this reference does not point to any valid memory, so the value of each array element is null. This means that students array elements cannot be directly used, because each array element is null, which is equivalent to defining two consecutive Person variables, but this variable has not yet pointed to any valid memory zone, therefore, these two consecutive Person variables (the array elements of the students array) are not available.

The following code defines two Person instances, zhang and lee. It defines that the two instances actually allocate four memories and store the reference variables zhang and lee in the stack memory, two Person instances are also stored in the heap memory. The memory storage diagram is as follows:

At this time, the two array elements of the students array are still null until the program assigns zhang to the first element of the students array in sequence and assigns lee to the second element of the students array, the two array elements of the students array point to a valid memory zone. The memory storage diagram is shown in:

It can be seen from: At this time, zhang and students [0] point to the same memory zone, and they all reference type variables, therefore, the attributes and methods used to access the Person instance through zhang and students [0] have the same effect, regardless of modifying the attributes of the Person instance pointed to by students [0, or modify the attributes of the Person instance pointed to by the zhang variable. The modified attributes are actually in the same memory zone, so they will inevitably affect each other. Similarly, lee and students [1] are also referenced to the same Person object, with the same effect.

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.