In-depth understanding of pass-by-value and pass-by-reference in java

1. Value Types and Reference Types

  1. Storage in the stack:

  • Value types are stored on the stack by default, but when a value type is declared in a reference type, it is stored in the heap of the reference type where it is located.

  • Reference types are stored on the heap. Its memory address in the heap is stored in the stack.

        

  2. Parameter passing method

  • Value type parameters can be passed by value, or modified by the ref and out keywords to pass by reference.

  • Reference type parameters can only be passed by reference.

2. Pass-by-value and pass-by-reference

  1. Passing by value

     Pass-by-value is to pass a copy of the variable to the method, and how to manipulate the copy of the variable in the method will not change the value of the original variable.

     


   in the example below. The variable a is passed to the method Test() by value. When Test executes the a++ operation, it actually operates on the copy of a. The value of a is printed in the Main method, and the result is still a=1.

copy code

1     class Program
 2     {
 3         public static void Main(string[] args)
 4         {
 5             int a = 1;
 6             Test(a);
 7             Console.WriteLine(a);
 8             
 9             Console.Write("Press any key to continue . . . ");
10             Console.ReadKey(true);
11         }
12         
13 // value passing
14         static void Test(int a)
15         {
16             a++;
17         }
18     }


    Result: 1

copy code

  2. Pass by reference

        Pass-by-reference is to pass the memory address of a variable to a method, and when the method manipulates the variable, it will find the variable stored at that address and operate on it. will affect the original variable.

      The term "original variable" is used here only to illustrate the comparison with value transfer. In fact, all methods operate on the same object, and there should be no "original variable".

        In the example, the Person object p and the variable a (modified by the ref keyword) are passed by reference to the method Test(). When Test operates on the variable, it finds p in the heap through the passed address 010x and operates on it. So the result is printed again in the Main function, which has changed.

copy code

1     class Program
 2     {
 3         public static void Main(string[] args)
 4         {
 5             int a = 1;
 6             Person p=new Person{Age=20};
 7 //Pass the value type variable a by reference through the ref keyword
 8             Test(ref a,p);
 9             Console.WriteLine(a);
10             Console.WriteLine(p.Age);
11             
12             Console.Write("Press any key to continue . . . ");
13             Console.ReadKey(true);
14         }
15         
16 //Pass by reference
17         static void Test(ref int a,Person p)
18         {
19             a++;
20             p.Age++;
21         }
22         
23     }
24     
25     class Person
26     {
27         public int Age{get;set;}
28     }


    Result: 2
       21

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325510406&siteId=291194637