The format of an object in memory. The object memory exists.

Source: Internet
Author: User

The format of an object in memory. The object memory exists.

When the program runs, it is divided into several areas in the memory:

Stack area: Class, object, interface, array stack area: basic data type (the String type will also be stored in the stack Area)

Global zone: stores the global data constant zone: stores the constant code zone: stores the code

See the following code.

Public class PersonOBJ {public static void main (String [] args) {class Person {int age; String name;} Person a = new Person ();. age = 10;. name = "James"; Person B =;
System. out. println (a. name); System. out. println (B. name );}}

Memory diagram:

The storage address is in hexadecimal format. The storage address is similar to the room door number, and the variable (object) is equivalent to the room number. The hexadecimal number of Address allocation is not, but depends on the distribution of the operating system, this is just for convenience.

Person a stores the address of an object (new Person () (for example, [0x123]), and Person a also has an address (for example, [0x111]). for convenience, Person a stores the address of a (new Person () object (Person a [0x123]).

Person a = new Person (); a new heap space will be opened in the memory. The heap space will store a Person a object with the Person class attribute whose storage address is 0x123.

After creating an object in new Person ();, assign it to the Person a variable in the stack zone. Person a will reference the object new Person (); the address of the heap zone [0x123]

Access and assign values to object attributes (. age = 10), the address of the heap area where the object is located is [0x123], and the corresponding attribute value is assigned;. name = "James" and so on.

Person B =;

Person B is still in the stack. Assigning a variable (object) to Person B actually assigns the address of the heap class [0x123]. that is to say, the two variables point to the same address of the heap space, that is why we see the two James.

 

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.