Java talking about the memory analysis of arrays (II.)

Source: Internet
Author: User

Initialization of an array of reference types

1 An array element is a reference when the memory is parsed

 PackageCom.java.array;classperson{ Public intAge//Age     Public DoubleHeight//Height//Define an info method     Public voidinfo () {System.out.println ("My Age is:" +age+ ", My height is:" +height); }} Public classArrayTest2 { Public Static voidMain (string[] args) {//defines a student array variable whose type is person[]Person []students; //perform dynamic initializationstudents=NewPerson[2]; //Create a person instance and assign the instance to the variable ZhangPerson zhang=NewPerson (); //Assigns the age and height of the person object referenced by ZhangZhang.age=18; Zhang.height=175; //Create a person instance and assign the instance to the variable WangPerson wang=NewPerson (); //Assigns the age and height of the person object referenced by ZhangWang.age=24; Wang.height=170; //assign the value of the Zhang variable to the first array elementstudents[0]=Zhang; //Assigning a Wang variable to the second array elementstudents[1]=Wang; //The result of the following two lines of code is exactly the same.//because Wang and students[1] point to the same person instanceWang.info (); students[1].info (); }}

The code above translates to a storage description:

Java talking about the memory analysis of arrays (II.)

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.