Java value transfer and reference transfer, java transfer reference
Tian tianbo planted a big pitfall in the field of value transfer and reference transfer, resulting in a waste of time in the afternoon. let's first talk about passing and referencing values to the official explanation of java:
Value Transfer: (The formal parameter type is the basic data type): When a method is called, the actual parameter passes its value to the corresponding formal parameter, the format parameter only initializes the content of the storage unit with the actual value of the parameter. It is two different storage units. Therefore, changing the format parameter value during method execution does not affect the actual value of the parameter.
Reference transfer: (The formal parameter type is the reference data type parameter), also known as the transfer address. When a method is called, the actual parameter is an object (or an array). In this case, the actual parameter and the formal parameter point to the same address. During method execution, the operation on the formal parameters is actually an operation on the actual parameters. This result is retained after the method ends. Therefore, changes to the formal parameters during method execution will affect the actual parameters.
In general, value transfer is actually a transmission of some specific data, that is, the basic type of data (, "Haha "....), the reference transfer is the object that you pass in your own package, or the data set, list, array, and set...
All the good things I 've encountered about list data processing come with code (Test Code) if they don't match each other ):
1. the following code is a demo I wrote from myself. It details the value transfer and reference transfer:
1 @ Test 2 public void ck () {3 bean B = new bean ("King", "1"); 4 bean b2 = new bean ("King ", "2"); 5 List <bean> list = new ArrayList <bean> (); 6 List <bean> list2 = new ArrayList <bean> (); 7 list. add (B); 8 list. add (b2); 9 10 list2.addAll (list); 11 list2.get (0 ). setName ("HAHAHA"); 12 13 for (bean: list2) {14 System. out. println (bean. getName () + "ssss" + bean. getAge (); 15} 16 for (bean bean1: list) {17 System. out. println (bean1.getName () + "jjjjj" + bean1.getAge (); 18} 19 20}
Bean class
1 public class bean { 2 3 private String name ; 4 private String age; 5 6 7 8 9 10 11 public bean() {12 super();13 }14 public bean(String name, String age) {15 super();16 this.name = name;17 this.age = age;18 }19 public String getName() {20 return name;21 }22 public void setName(String name) {23 this.name = name;24 }25 public String getAge() {26 return age;27 }28 public void setAge(String age) {29 this.age = age;30 } 31 }
In our normal thinking, the list should output two
List:
Wang ssss1
Wang ssss2
List2:
Hahaha ssss1
Wang ssss2
However: view my running results
1 hahaha ssss12 Wang ssss23 hahaha jjjjj14 Wang jjjjj2
We can see that although only one element in list2 has been modified, the element data in the list has also been modified. Some may say "list. the addAll problem is actually not. The key is bean, list2.addAll (list)
This method is actually the memory pointer of the bean assigned to list2, instead of copying the value to the memory block of list2, that is, reference transfer occurs here rather than value transfer, therefore, if the value of the element in list2 is modified
The value of the bean object element will certainly change the value of the list output element. Then I solved it like this:
1 @ Test 2 public void ck () {3 bean B = new bean ("King", "1"); 4 bean b2 = new bean ("King ", "2"); 5 List <bean> list = new ArrayList <bean> (); 6 List <bean> list2 = new ArrayList <bean> (); 7 list. add (B); 8 list. add (b2); 9 10 for (int I = 0; I <list. size (); I ++) {11 bean bn = new bean (); 12 bn. setAge (list. get (I ). getAge (); 13 bn. setName (list. get (I ). getName (); 14 list2.add (bn); 15} 16 list2.get (0 ). setName ("HAHAHA"); 17 18 for (bean: list2) {19 System. out. println (bean. getName () + "ssss" + bean. getAge (); 20} 21 for (bean bean1: list) {22 System. out. println (bean1.getName () + "jjjjj" + bean1.getAge (); 23} 24 25} 26
When the list value is assigned to list2, I add a new bean object and traverse the bean attribute values in the list to the new bean object, in this way, the basic data type is passed, that is, the data is copied to the new bena memory block, so how to modify list2 will not affect the value in the list, because the bean objects they bear with are two different pointer objects, This is the output result after modification:
1 list2: 2 hahaha ssss13 Wang ssss24 list: 5 Wang jjjjjj16 Wang jjjjj2
My personal understanding, what other easier methods do you have and learn from each other?