Note: I misunderstood the reference for passing parameters.

Source: Internet
Author: User
Most of the time, the difference between the reference type and the value type is obvious, but when the reference type is used as the method parameter, it has caused many misunderstandings, every time I get mad at my computer, I find the problem again.

The problem is: Code Section: Code segment 1
Public   Class Person {
Public IntAge;
}


Public   Static   Void Main ( String [] ARGs)
{
Person P
= NewPerson ();//#1
P. Age= 10;
Addagea (P );
Console. writeline (P. Age );
}


Public   Static   Void Addagea (person P) {
P. Age
= 20;
}

At this time, 20 will be output, which is obvious. Now we add a method addageb:Public static void addageb (person P)
Public   Static   Void Addageb (person P) {
P
= NewPerson ();//#2
P. Age= 20;
}

When addagea (p) in the main method is changed to addageb (P), 10 is output instead of 20.

So I said to myself: Isn't class A reference type? Addageb receives a person of the reference type. Although a person is added in the method body, it is assigned a value to the age field of the reference type just like the following sentence?
In the main method, when person P = new person () (#1), a space is allocated in the heap, the next sentence is to store 10 to the memory space of the age in this space, and then pass the reference P to the addageb () method, in addageb () in the heap, a new space (#2) is opened, and the newly opened memory address is assigned to P. In the next sentence, the age in the new space is assigned to 20, so, should the output in main () be 20? Isn't that true?

After I went crazy, I found that a very important detail was missing from my self-talking above, and this detail is the key. The problem lies in this sentence: "then pass the reference P to the addageb () method", which is too vague, I forgot that the "reference transfer" here is actually a "copy" process. In the above two addage method bodies, P is actually only a copy of P in the main method. When executing: person P = new person (), an address space will be opened on the hosting stack, for an instance that stores the person, the age field of the instance exists in this space. At the same time, a memory space will be allocated on the stack to store the heap memory address of the person instance, the value of P is the address on the stack. When addagea (p) or addageb (P) is executed, the value of P on the stack (memory address) is copied, and the value of P in the addage method body is, the copied copy is not the original p. However, their values (addresses) are the same and all point to the person instance address in the managed heap. Therefore, in addagea () when executing P. when age = 20, the value of the age field of the person instance in the managed heap is 20. Therefore, when the main method is output, because P in the main method also points to the heap space address, 20 is output.

In addageb (), there is a key sentence: P = new person (), which will open up a new memory space in the managed heap, after the address is assigned to the P on the stack, the P in the main method and the P in the addageb method are not only in different positions on the stack, but also in different values, when executing the following sentence: P. age = 20; the value of age in the new heap space is indeed changed, but after the right curly braces of the method body are displayed, P (on the stack) in the method body is released because of the scope. Then, the person instance created in the addageb method becomes a garbage waiting for GC to be recycled, because there are two person instances in the heap, the age in the heap space pointed to by P in the main method is not changed, so 10 is output in the main method.

Let's look back at the packing:

Box main
Public   Static   Void Main () {
Int A =   5 ;
Object OBJ = A;
OBJ
=   10 ;
Console. writeline (
" A = "   + A );
Console. writeline (
" OBJ = "   + OBJ );
}

Obviously, a = 5 and OBJ = 10 are output.
In the second row, there is a packing operation. The value of A is copied to OBJ, so the value of a remains unchanged. The value of OBJ changes after OBJ = 10 is executed.

Now let's change it: Public   Static   Void Main () {
Int A =   5 ;
Object OBJ = A;
Change (OBJ );
Console. writeline (
" A = "   + A );
Console. writeline (
" OBJ = "   + OBJ );
}

Public   Void Change ( Object OBJ) {
OBJ
= 10;
}

What will be output at this time?
The correct answer is a = 5, OBJ = 5

It is also known that when OBJ = 10 is executed in the change method body, 10 will be loaded into the boxes in the managed heap, then, the OBJ value is assigned to the start address of the box. Because the OBJ reference value in the method body is different from the OBJ reference value in the main method, the output obj is still 5.

To sum up, there are several words:

If the ref or out keyword is not used, a copy process is performed when parameters are passed to the method, whether it is a value type or a reference type.

Of course, if ref is used, there will be no such replication process. The P in the main method and the P in the addage method are actually the same reference.

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.