Stored Procedure of reference data in memory

Source: Internet
Author: User

The storage of basic data in the memory is very simple. The reference type is to put the object address in the stack memory and the data in the heap memory. Here, a few small programs are used to briefly describe the stored procedure of referenced data in the memory:
Class Person {
String name;
Int age;
Void say (){
System. out. println ("name:" + name + ", age:" + age );
}
}
Public class Demo01 {
Public static void main (String args []) {
Person p1 = new Person ();
Person p2 = new Person ();
P1.name = "James ";
P1.age = 20;
P2 = p1;
P1.say ();
P2.say ();
P2.name = "Li Si ";
P2.age = 40;
P1.say ();
P2.say ();
}
}
The above program first creates a Person class, which has two member variables and a member method. Then a Demo01 class is created. Run the following code:
Person p1 = new Person;
Person p2 = new Person ();
The two Person instance objects will be allocated with memory space, and the two objects will be initialized. Memory Allocation 1 is shown in:
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'height =" 351 "alt =" "src =" http://www.bkjia.com/uploads/allimg/131228/140Q2Eb-0.jpg "width =" 468 "border =" 0 "/>
Run
P1.name = "James ";
P1.age = 20; assign a value to instance object p1, as shown in Memory Distribution 2:
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'height =" 346 "alt =" "src =" http://www.blogjava.net/images/blogjava_net/tscfengkui/2.jpg "width =" 450 "border =" 0 "/>
Run
P2 = p1; the Instance Object p2 is directed to the memory space of the Instance Object p1. The memory space originally allocated to the Instance Object p1 will be lost, so that when the system executes garbage GC) this part of memory will be released during technology. The memory is shown in step 3:
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'height =" 337 "alt =" "src =" http://www.blogjava.net/images/blogjava_net/tscfengkui/3.jpg "width =" 495 "border =" 0 "/>
Run
P1.say ();
P2.say ();
The output is as follows:
Name: Zhang San, age: 20
Name: Zhang San, age: 20
Run
P2.name = "Li Si ";
P2.age = 40;
The original value will be overwritten, as shown in memory allocation 4:
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'height =" 337 "alt =" "src =" http://www.blogjava.net/images/blogjava_net/tscfengkui/4.jpg "width =" 456 "border =" 0 "/>
Run
P1.say ();
P2.say ();
The output
Name: Li Si, age: 40
Name: Li Si, age: 40
This is the Stored Procedure for referencing type data. Java programs use this storage method for two reasons:
1. stack memory access speed is very fast, second only to registers. 2. It is connected to the garbage collection mechanism GC, which helps the mechanism to delete heap memory data that has not been pointed to and release memory space.
The above Program Execution
P2 = p1;
After this statement, p2 points to the data pointed to by p1 and cannot complete the copy function. We can complete the copy function with a slight modification. The procedure is as follows:
Class Person {
String name;
Int age;
Void say (){
System. out. println ("name:" + name + ", age:" + age );
}
}
Public class Demo02 {
Public static void main (String args []) {
Person p1 = new Person ();
Person p2 = new Person ();
P1.name = "James ";
P1.age = 20;
P2.name = p1.name;
P2.age = p1.age;
P1.say ();
P2.say ();
P2.name = "Li Si ";
P2.age = 40;
P1.say ();
P2.say ();
}
}
The output result of this program is:
Name: Zhang San, age: 20
Name: Zhang San, age: 20
Name: Zhang San, age: 20
Name: Li Si, age: 40
Let's analyze its memory stored procedures and execute code
Person p1 = new Person ();
Person p2 = new Person ();
P1.name = "James ";
P1.age = 20;
Its stored procedures 1 and 2 are the same, and the code is executed.
P2.name = p1.name;
P2.age = p1.age;
It does not point p2 to the data that p1 points to, but instead copies the data that p1 points to the data that p1 points. Its memory allocation process is as follows:
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'height =" 336 "alt =" "src =" http://www.blogjava.net/images/blogjava_net/tscfengkui/5.jpg "width =" 459 "border =" 0 "/>
Execute Code
P1.say ();
P2.say ();
The output
Name: Zhang San, age: 20
Name: Zhang San, age: 20
Execute Code
P2.name = "Li Si ";
P2.age = 40;
The data value pointed to by p2 is modified, but the data value pointed to by p1 is not modified. The memory allocation process is as follows:
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'height =" 338 "alt =" "src =" http://www.blogjava.net/images/blogjava_net/tscfengkui/6.jpg "width =" 452 "border =" 0 "/>
Execute Code
P1.say ();
P2.say ();
The output
Name: Zhang San, age: 20
Name: Li Si, age: 40
Next let's look at the next program
Class Person {
String name;
Int age;
Public void setName (String name ){
This. name = name;
}
Public void setAge (int age ){
This. age = age;
}
Public String getName (){
Return name;
}
Public int age (){
Return age;
}
Public void say (){
System. out. println ("name:" + name + ", age:" + age );
}
}
Public class Demo03 {
Public static void main (String args []) {
Person [] per = new Person [2];
Per [0] = new Person ();
Per [1] = new Person ();
Per [0]. setName ("James ");
Per [0]. setAge (20 );
Per [1]. setName ("Li Si ");
Per [1]. setAge (40 );
For (int I = 0; I <per. length; I ++ ){
Per [I]. say ();
}
}
}
The output result of the above program is
Name: Zhang San, age: 20
Name: Li Si, age: 40
This program first creates a Person class, which contains member variables and member methods, then defines a Demo03 class, and executes the code
Person [] per = new Person [2];
Per [0] = new Person ();
Per [1] = new Person ();
Create an array of Person objects and initialize the object. The initialization here only points the array element to null, but does not allocate space in the heap memory, as shown in memory storage 7:
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'height =" 279 "alt =" "src =" http://www.blogjava.net/images/blogjava_net/tscfengkui/7.jpg "width =" 336 "border =" 0 "/>
Execute Code
Per [0] = new Person ();
Per [1] = new Person ();
This opens up heap memory space for array elements and initializes them, as shown in Figure 8:
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'height =" 339 "alt =" "src =" http://www.blogjava.net/images/blogjava_net/tscfengkui/8.jpg "width =" 466 "border =" 0 "/>
Execute Code
Per [0]. setName ("James ");
Per [0]. setAge (20 );
Per [1]. setName ("Li Si ");
Per [1]. setAge (40 );
Store a piece of data to the heap memory pointed to by each array element, as shown in Figure 9:
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'height =" 328 "alt =" "src =" http://www.blogjava.net/images/blogjava_net/tscfengkui/9.jpg "width =" 451 "border =" 0 "/>
Execute Code
For (int I = 0; I <per. length; I ++ ){
Per [I]. say ();
}
Is to output the values in the array, the above output results will be output.
The stored procedure of the java reference type is like this. This is only the next small insight, which may not be in place or incorrect. You are welcome to give your comments and share your knowledge.

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.