Java_se Foundation--24. Object-Oriented memory analysis

Source: Internet
Author: User

Dark Horse Programmer Entry Blog ...


Then the previous chapter of the Code:

Class car{//the public properties of a thing are described using member variables. Stringname; The attribute of the name Stringcolor; Color attribute int wheel;//number of wheels//common behavior of things use function description. public void Run () {System.out.println (name+) "Fast Running ..."}} Class Demo1{public static void Main (string[] args) {//uses the car class to declare a C variable, and the C variable points to a car object. Carc = new Car (); Sets the property value of the car object. C.name = "BMW"; c.color = "white"; C.wheel = 4;//The property value of the vehicle object System.out.println ("Name:" + c.name+ "color:" + c.color+ "number of Wheels:" +c.wheel); c . Run (); }}
The result is:

Name: BMW color: White Wheel Number: 4

BMW is running fast.



How does the above code work in memory?

Let me give you the following analysis:


C is a local variable

1. First declare the C variable allocated in the stack memory

2.new car creates a vehicle object in heap memory to see if car has those member properties, and then assigns an initial value to the object, name=null,color=null,wheel=0 (once the member is created, the member variable of the object is assigned the default initial value immediately) and then the address 0x9 7 gave C c the object that pointed to the car.

3.

C.name was the object C took the address to find the car and changed the name to BMW.

C.color was the object C took the address to find the car and changed the color to white.

C.wheel was C, took the address to find the car object and then changed the wheel to 4.



Let's look at an example below:

Class car{//the public properties of a thing are described using member variables. Stringname; The attribute of the name Stringcolor; Color attribute int wheel;//number of wheels//common behavior of things use function description. public void Run () {System.out.println (name+) "Fast Running ..."}} Class Demo1{public static void Main (string[] args) {Car C1 = new Car (); Car C2 = new car (); c1.name = "BMW"; c2.name = "Volkswagen"; System.out.println ("Name:" + c1.name);}  }

Result: Name: BMW

See Explanation:

1. First declare the C1 variable allocated in the stack memory

2.new car creates a vehicle object in heap memory to see if car has those member properties, and then assigns an initial value to the object, name=null,color=null,wheel=0 (once the member is created, the member variable of the object is assigned the default initial value immediately) and then the address 0x9 7 gave the C1 C1 the object of the car.

3. Re-declared the C2 variable allocated in the stack memory

4. New a car in the heap memory to create a vehicle object to see if car has those member properties, and then assigns the initial value to the object, for Name=null,color=null,wheel=0 (once the member is created, the member variable of the object will be assigned the default initial value immediately) and then the ground Address 0x67 assigned to C2 C2 pointed to the object of the car.

5. C1.name was the object C1 took the address to find the car and changed the name to "BMW."

6. C2.name was the object C2 took the address to find the car and changed the name to "Volkswagen."

7. Last output C1.name C1 just take the address and find the corresponding object. Outputs the name of the object

So the result: BMW

Let's change the example below:

Class car{//the public properties of a thing are described using member variables. Stringname; The attribute of the name Stringcolor; Color attribute int wheel;//number of wheels//common behavior of things use function description. public void Run () {System.out.println (name+) "Fast Running ..."}} Class Demo1{public static void Main (string[] args) {Car C1 = new Car (); c1.name = "BMW"; Car C2 = new car (); c2.name = "Volkswagen"; c1 = C2; System.out.println ("Name:" + C1.name);}}

Results: Volkswagen

Explain:


1. First declare the C1 variable allocated in the stack memory

2.new car creates a vehicle object in heap memory to see if car has those member properties, and then assigns an initial value to the object, name=null,color=null,wheel=0 (once the member is created, the member variable of the object is assigned the default initial value immediately) and then the address 0x9 7 gave the C1 C1 the object of the car.

3. C1.name was the object C1 took the address to find the car and changed the name to "BMW."

4. Re-declared the C2 variable allocated in the stack memory

5. New a car in the heap memory to create a vehicle object to see if car has those member properties, and then assigns the initial value to the object, for Name=null,color=null,wheel=0 (once the member is created, the member variable of the object will be assigned the default initial value immediately) and then the ground Address 0x67 assigned to C2 C2 pointed to the object of the car.

6. C2.name was the object C2 took the address to find the car and changed the name to "Volkswagen."

7. C2 's holding address was assigned to C1 C1 's address originally 0x97 changed to 0x67

8. Final output C1.name C1 take the changed address to find the property name in the object with the address of 0x67 output to the 0x67 address.

So the result is: the public

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Java_se Foundation--24. Object-Oriented memory analysis

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.