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.