1: What is passing by value
It means that when a method is called, the passed parameters are passed by copy of the value. Examples are as follows:
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 to pass by value} }Important characteristics of passing by value: what is passed is a copy of the value, which means that it is not related to each other after passing.
Examples are as follows:
public class TempTest { private void test1(int a){ a = 5; System.out.println("test1 method a="+a); } public static void main(String[] args) { TempTest t = new TempTest(); int a = 3; t.test1(a);//After passing, the change of the variable value of the test1 method does not affect the a System.out.println("main method a="+a); } }The result of the operation is:
a=5 in test1 method a=3 in main method
2: What is passing by reference
It refers to the passed parameters when the method is called, and the passed parameters are passed by reference. In fact, the passed reference address is the address of the memory space corresponding to the variable.
Examples are as follows:
public class TempTest { private void test1(A a){ } public static void main(String[] args) { TempTest t = new TempTest(); A a = new A(); t.test1(a); //The parameter a passed here is to pass by reference} } class A{ public int age = 0; }3: Important characteristics of passing by reference
What is passed is a reference to the value, which means that both before and after passing point to the same reference (that is, the same memory space).
Examples are as follows:
public class TempTest { private void test1(A a){ a.age = 20; age="+a.age in System.out.println("test1 method="+a.age); } public static void main(String[] args) { TempTest t = new TempTest(); A a = new A(); a.age = 10; t.test1(a); age="+a.age in System.out.println("main method="+a.age); } } class A{ public int age = 0; }The operation results are as follows:
age=20 in test1 method, age=20 in main method
4: Understand the process of passing by reference - memory allocation diagram
To correctly understand the process of passing by reference, you must learn to understand the process of memory allocation, and the memory allocation diagram can help us understand this process.
Use the above example to analyze:
(1): Start running, run line 8, create an instance of A, and the memory allocation diagram is as follows:
(2): Run line 9, modify the value of age in instance A. The memory allocation diagram after running is as follows:
(3): Run line 10, which is to pass the memory space address referenced by variable a in main method to the variable a in test1 method according to the reference. Please note: these two a variables are completely different, don't be fooled by the same name.
Memory allocation is as follows:
Since it is passed by reference, that is, the address of the memory space is passed, the new memory diagram formed after the transfer is completed is as follows:
In other words: both variables point to the same space.
(4): Run line 3 and assign the value to the age of the instance A pointed to by variable a in the test1 method. The new memory diagram formed after completion is as follows:
At this time, the change in the age value of instance A is caused by the test1 method
(5): Run line 4, and according to the memory diagram at this time, output age=20 in the test1 method
(6): Run line 11, and according to the memory diagram at this time, output age=20 in the main method
5: Changes to the above examples
After understanding the above example, some people may ask, can the values passed by reference not affect each other? What is the modification in the test1 method that does not affect the main method?
The method is to just use a new instance in the test1 method. Change to the following example, where the third behavior is newly added:
public class TempTest { private void test1(A a){ a = new A();//A new line a.age = 20; System.out.println("test1 method age="+a.age); } public static void main(String[] args) { TempTest t = new TempTest(); A a = new A(); a.age = 10; t.test1(a); System.out.println("main method age="+a.age); } } class A{ public int age = 0; }The running result is:
age=20 in test1 method age=10 in main method
Why is the result of this run different from the previous example? It is better to use memory diagram to understand it.
6: Understand by reference again
(1): Start running, run line 9, create an instance of A, and the memory allocation diagram is as follows:
(2): Run line 10 is to modify the value of age in instance A. The memory allocation diagram after running is as follows:
(3): Running line 11 is to pass the memory space address referenced by variable a in main method to the variable a in test1 method according to the reference. Please note: these two a variables are completely different, don't be fooled by the same name.
Memory allocation is as follows:
Since it is passed by reference, that is, the address of the memory space is passed, the new memory diagram formed after the transfer is completed is as follows:
In other words: both variables point to the same space.
(4): Run line 3 and regenerate a new instance of A for variable a in test1 method. The new memory diagram formed after completion is as follows:
(5): Run line 4 to assign the value to the age of the new instance A pointed to by variable a in the test1 method. The new memory diagram formed after completion is as follows:
Note: At this time, the age of the variable a in the test1 method is changed, while the main method does not change.
(6): Run line 5, and according to the memory diagram at this time, output age=20 in test1 method
(7): Run line 12, and according to the memory diagram at this time, output age=10 in the main method according to the output memory diagram = 10
7: Explanation
(1): "In Java, parameter passing is passed by value" means: passing by value is a copy of the passed value. Passing by reference actually passes the reference address value, so it is collectively called passing by value.
(2): In Java, only the basic types and Strings according to the following definition method are passed by value, and the others are passed by reference. It is to directly define strings using double quotes: String str = "Java Private School";
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.