A detailed description of value passing and reference passing:
1: what is passed by value
Refers to when a method call passes a parameter that is passed by a copy of the Value. Examples are as Follows:
[java]View PlainCopy
- Public class Temptest {
- Private void test1 (int A) {
- Do Something.
- }
- Public static void main (string[] Args) {
- Temptest t = new Temptest ();
- int a = 3;
- T.test1 (a); //the parameter A passed here is passed by value
- }
- }
Passing important characteristics by value: passing a copy of a value, that is, passing it off is Unrelated.
Examples are as Follows:
[java]View PlainCopy
- Public class Temptest {
- Private void test1 (int A) {
- A = 5;
- System.out.println ("a= in the Test1 method" +a);
- }
- Public static void main (string[] Args) {
- Temptest t = new Temptest ();
- int a = 3;
- T.test1 (a); ///pass, The Test1 method changes the value of the variable without affecting the A
- System.out.println ("a= in the main method" +a);
- }
- }
The operating result is:
[java]View PlainCopy
- A=5 in The Test1 method
- A=3 in The Main method
2: what is passed by reference
When a method call is made, the passed argument is passed by reference, in fact the address of the reference that is passed, that is, the address of the memory space that the variable corresponds to.
Examples are as Follows:
[java]View PlainCopy
- Public class Temptest {
- Private void Test1 (a A) {
- }
- Public static void main (string[] Args) {
- Temptest t = new Temptest ();
- A = new a ();
- T.test1 (a); //the parameter A passed here is passed by reference
- }
- }
- Class a{
- public int age = 0;
- }
3: important features passed by reference
A reference to a value is passed, meaning that both before and after delivery point to the same reference (that is, the same memory space).
Examples are as Follows:
[java]View PlainCopy
- Public class Temptest {
- Private void Test1 (a A) {
- A.age = 20;
- System.out.println ("age= in the Test1 method" +a.age);
- }
- Public static void main (string[] Args) {
- Temptest t = new Temptest ();
- A = new a ();
- A.age = 10;
- T.test1 (a);
- System.out.println ("age= in the main method" +a.age);
- }
- }
- Class a{
- public int age = 0;
- }
The results of the operation are as Follows:
[java]View PlainCopy
- Age= in the Test1 method
- Age= in the Main method
4: Understanding the process of passing by Reference-memory allocation
To understand correctly the process of passing by reference, you must learn to understand the memory allocation process, and memory allocation can help us to understand the PROCESS.
Use the example above to Analyze:
(1): run start, Run line 8th, Create an instance of a, memory allocation schematic as Follows:
(2): Run line 9th, is to modify the value of age in a instance, after running memory allocation is as Follows:
(3): Running line 10th is the memory space address referenced by the variable A in the main method, passed by reference to the A variable in the Test1 method. Please note: these two a variables are completely different and should not be blinded by the same name.
Memory allocations are as Follows:
Because it is passed by reference, that is, the address of the memory space is passed, so the new memory formed after delivery is as Follows:
That is, two variables all point to the same space.
(4): Run Line 3rd, Assign a value to the age of the A instance that the variable a in the Test1 method points to, and the new memory that is formed after completion is as Follows:
The change in the age value of the A instance is caused by the test1 Method.
(5): Run line 4th, according to the memory at this time, the output test1 method of age=20
(6): Run line 11th, according to the memory at this time, output age=20 in the Main method
5: changes to the above example
Understanding the above example, one might ask, can you let the values passed by reference not affect each other? Is the change in the Test1 method does not affect the main method inside it?
A new instance of the Test1 method is Available. Change into the following example, where the 3rd Act is added:
[java]View PlainCopy
- public class Temptest {
- private void Test1 (a a) {
- A = new a (); Add a new line
- A.age = 20;
- System.out.println ("age= in the Test1 method" +a.age);
- }
- public static void main (string[] Args) {
- Temptest t = new Temptest ();
- A = new a ();
- A.age = 10;
- T.test1 (a);
- System.out.println ("age= in the main method" +a.age);
- }
- }
- Class a{
- public int age = 0;
- }
The result of the Operation Is:
[java]View PlainCopy
- Age= in the Test1 method
- Age= in the Main method
Why is the result of this operation not the same as the previous example, or the use of memory to understand
6: understand again by reference delivery
(1): run start, Run line 9th, Create an instance of a, memory allocation schematic as Follows:
(2): Run line 10th, is to modify the value of age in a instance, after running memory allocation is as Follows:
(3): Running line 11th is the memory space address referenced by the variable A in the main method, passed by reference to the A variable in the Test1 method. Please note: these two a variables are completely different and should not be blinded by the same name.
Memory allocations are as Follows:
Because it is passed by reference, that is, the address of the memory space is passed, so the new memory formed after delivery is as Follows:
That is, two variables all point to the same space.
(4): Run line 3rd, for the Test1 method in the variable a re-generated a new instance of a, the completion of the formation of the new memory as Follows:
(5): Run Line 4th, Assign a value to the age of the new A instance that the variable a in the Test1 method points to, and the new memory that is formed after completion is as Follows:
Note: The age of variable A in the Test1 method is changed at this time, and the main method is Unchanged.
(6): Run line 5th, according to the memory at this time, the output test1 method of age=20
(7): Run line 12th, according to the memory at this time, output age=10 in the Main method
7. Description:
1. An object is a reference: passing by value means that when a parameter is passed to a function, the function receives a copy of the original value
2. The original type is the pass value: passing by reference means that when a parameter is passed to a function, the function receives the memory address of the original value, not the copy of the value
3.string,integer, double, etc. immutable types because they do not provide their own modified functions, each operation is a new object, so special Treatment. Can be considered as a pass Value.
Integer is the same as String. The class variable that holds the value is the final attribute, cannot be modified, and can only be re-assigned/generated new Object. When an integer is passed into the method as a method parameter, its assignment causes the reference of the original integer to be pointed to the stack address within the method and loses its point to the original class variable Address. Any action done on the assigned integer object does not affect the original Object.
Reference: http://blog.csdn.net/zzp_403184692/article/details/8184751 (previous)
http://blog.csdn.net/fbysss/article/details/3082949 (later)
Thank you for the classic summary of our original!
(rpm) Java: Pass-by-value and pass-by-reference detailed explanations