Java function parameter passing Detailed

Before reading this article, based on their experience and understanding, we can start to think about the way of passing parameters and select Java functions: 
A. is passed by value? 
B. passed by reference? 
C. section by section by reference value? 
Temporarily announced the correct answer here, we adopted a simple example for everyone to find the answers: 
1. The first definition of a type Value 

Java code  Collection Code

  1. public static class Value {  
  2.     private String value = "value";  
  3.     public String getValue() { return value; }  
  4.     public void setValue(String value) { this.value = value; }  
  5. }  


2. Write two functions newValue and modifyValue: setValue method will newValue into the reference point to a new object, modifyValue calls to modify the parameters of the value of the object of value. 

Java code  Collection Code

  1. public static void newValue(Value value) {  
  2.     value = new Value();  
  3.     value.setValue("new value");  
  4.     System.out.println("In newValue, HashCode = " + value.hashCode() + ", value = " + value.getValue());  
  5. }  
  6.       
  7. public static void modifyValue(Value value) {  
  8.     value.setValue("new value");  
  9.     System.out.println("In modifyValue, HashCode = " + value.hashCode() + ", value = " + value.getValue());  
  10. }  


3. simple test code 

Java code  Collection Code

  1. public static void main(String[] args) {  
  2.     Value value1 = new Value();  
  3.     System.out.println("Before modify, HashCode = " + value1.hashCode() + ", value = " + value1.getValue());  
  4.     // value1 will point to the new Value Object  
  5.     newValue(value1);  
  6.     System.out.println("After modify, HashCode = " + value1.hashCode() + ", value = " + value1.getValue() + "\n");  
  7.     Value value2 = new Value();  
  8.     System.out.println("Before modify, HashCode = " + value2.hashCode() + ", value = " + value2.getValue());  
  9.     // set methods use the object, modify the object values ​​of the internal  
  10.     modifyValue(value2);  
  11.     System.out.println("After modify, HashCode = " + value2.hashCode() + ", value = " + value2.getValue());  
  12. }  


4. Results of the log: 

Java code  Collection Code

  1. Before modify, HashCode = 12677476, value = value  
  2. In newValue, HashCode = 33263331, value = new value  
  3. After modify, HashCode = 12677476, value = value  
  4.   
  5. Before modify, HashCode = 6413875, value = value  
  6. In modifyValue, HashCode = 6413875, value = new value  
  7. After modify, HashCode = 6413875, value = new value  



5. Analysis of Results: 
The code which is a very common programming mode: the definition of the peripheral | Save | Gets a value or object, the object as a parameter to a method for modifying object attributes in the method, behavior. But two methods newValue and modifyValue modification is not the same, after the method call, the object also has a great difference in the peripheral view! How to understand this difference? To ponder what passed by value, according to the concept passed by reference: 
* passed by value means that when a parameter passed to a function, the function receives a copy of the original value. Therefore, if a function modifies the parameters, only change the copy, and the original value remains unchanged. 
* Passed by reference means that when a parameter passed to a function, the function receives the memory address of the original value, rather than a copy of the value. Thus, if a function modifies the original value of the parameter, the parameter (calling code other than the function block) has changed. 
The correct answer: A - Java function arguments are passed by value!  
Analyze log: 
* After the first stage the log output, VALUE1 parameter is to point to a new object inside newValue method, and outputs the value hashCode values and new objects, but out newValue method field, the main method in VALUE1 does not occur any changes, which meets the definition and characteristics passed by value; if it is passed by reference, value1 after calling newValue (value value) method should be changed. 
* Log output second stage, carried out inside modifyValue value2 methods setValue the operation, while the hashCode constant value is modified, after leaving modifyValue method field, the main method in value2 change does occur. Used C ++ people tend to understand this phenomenon as follows: the transfer function parameters by reference! This is because like most with C ++, passed by reference! But Li Qiaqia is the place most likely to fall into the error of! 
Hidden behind different phenomena are two logs principle is: Java language is passed by value parameters passed by reference object; objects in Java are in fact operated by reference to the operation object, object itself is stored in the "reactor" in , the "reference" object saved in a register or "stack" of. 
Pseudocode describing what is different from the method and modifyValue newValue methods: 

Java code  Collection Code

  1. newValue{  
  2.     Value_ref2 = value_ref1; // value passed by reference value_ref1, get a copy of value_ref1  
  3.     value_obj2 = new Value (); // value_obj2 is created, initialized in the "heap" in  
  4.     value_ref2 -> value_obj2;    // value_ref2 指向value_obj2  
  5. value_ref2 -> value_obj2.setValue ( "xxx"); // value_obj2 the value is modified  
  6. printValueObj2 (); // print here is the value of obj2  
  7. }  
  8. modifyValue{  
  9.     Value_ref2 = value_ref1; // value passed by reference value_ref1, get a copy of value_ref1  
  10. value_ref2 -> value_obj1.setValue ( "xxx"); // value_obj1 the value is modified  
  11. printValueObj1 (); // print here is the value of obj1  
  12. }  


Clear enough! When value1_ref1 function as a parameter, first copied a copy function for value1_ref2 domain, in which case these two points is a value_obj ref same; the newObject function code [value = new Value ();] in fact, the value1_ref1 points to a new object value_obj2; set after this operation is the operation of the new object; modifyValue functions are operated directly by the method set value_obj1, it is with newValue function of the difference. 

Product is slightly Library http://www.pinlue.com/

Guess you like

Origin blog.csdn.net/yihuliunian/article/details/91368706