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.)