Before reading this article, according to your own experience and understanding, you can first think about and choose the Java function parameter passing way:
A. is passed by value?
B. Passed by reference?
C. Partial by Value section by reference?
We do not announce the correct answer here, we have a simple example for you to find the answer:
1. Define a Type value first
Java code
- Public static class Value {
- private String value = "value";
- Public String GetValue () { return value;}
- public void SetValue (String value) { this.value = value;}
- }
2. Write two functions newvalue and Modifyvalue:newvalue will point the incoming parameter to a new object, Modifyvalue will invoke the SetValue method of the incoming parameter to modify the value of the object.
Java code
- Public static void NewValue (value value) {
- Value = new value ();
- Value.setvalue ("new value");
- System.out.println ("in newvalue, hashcode =" + value.hashcode () + ", value =" + Value.getvalue ());
- }
- Public static void Modifyvalue (value value) {
- Value.setvalue ("new value");
- System.out.println ("in modifyvalue, hashcode =" + value.hashcode () + ", value =" + Value.getvalue ());
- }
3. Simple test Code
Java code
- Public static void Main (string[] args) {
- Value value1 = new value ();
- System.out.println ("before modify, Hashcode =" + value1.hashcode () + ", value =" + Value1.getvalue ());
- //Value1 point to the new value object
- NewValue (value1);
- System.out.println ("after modify, Hashcode =" + value1.hashcode () + ", value =" + value1.getvalue () + "\ n");
- Value value2 = new value ();
- System.out.println ("before modify, Hashcode =" + value2.hashcode () + ", value =" + Value2.getvalue ());
- //Use the Set method of object to modify the internal values of the objects
- Modifyvalue (value2);
- System.out.println ("after modify, Hashcode =" + value2.hashcode () + ", value =" + Value2.getvalue ());
- }
4. Execution result log:
Java code
- before modify, hashcode = 12677476, value = value
- In newvalue, hashcode = 33263331, value = NEW VALUE  
- after modify, hashcode = 12677476, value = value
-
- Before modify, hashcode = 6413875, VALUE = VALUE  
- In modifyvalue, hashcode = 6413875, value = new value
- after modify, hashcode = 6413875, Value = new value
5. Results Analysis:
The above code is a very common programming pattern: in the perimeter definition | save | Gets a value or object, passes the object as a parameter into a method, modifies the object's properties, behavior in the method. However, two methods NewValue and Modifyvalue are not modified in the same way, after the method call, the object in the periphery seems to have a very big difference! How do you understand this difference? First, the concept of passing by value, by reference:
* Passing by value means that when a parameter is passed to a function, the function receives a copy of the original value. Therefore, if the function modifies the parameter, only the copy is changed, and the original value remains unchanged.
* Passing by reference means that when a parameter is passed to a function, the function receives the memory address of the original value instead of a copy of the value. Therefore, if the function modifies the parameter, the original value of the parameter (in the calling code outside the function block) also changes.
the correct answer: The A--java function is passed the parameter by value!
Analyze the log:
* The first log output, the value1 parameter is changed inside the NewValue method to point to the new object, and output the new object's hashcode and value values, but after jumping out of the NewValue Method field, the value1 in the main method has not changed. This conforms to the definition and characteristics passed by value, and if passed by reference, value1 should be changed after calling the NewValue (value value) method.
* The second log output, value2 within the Modifyvalue method SetValue operation, hashcode unchanged and value is modified, after leaving the Modifyvalue method domain, in the main method value2 did change. People who have used C + + are apt to interpret this as: passing function arguments by reference! Because it's like passing by reference in C + +! But here is the most easy to get into the wrong place!
The underlying principle behind the different phenomena of two logs is that the Java language is passing parameters by value, passing objects by reference, objects in Java are actually references to operands, object itself is stored in the heap, and references to objects are saved in registers or "stacks".
Pseudo-code describes the difference between the NewValue method and the Modifyvalue method:
Java code
- newvalue{
- Value_ref2 = VALUE_REF1; //By value incoming reference value_ref1, get copy of Value_ref1
- Value_obj2 = new Value (); //Value_obj2 is created, initialized in "heap"
- Value_ref2, Value_obj2; //Value_ref2 point to Value_obj2
- Value_ref2->value_obj2.setvalue ("xxx"); //The value of Value_obj2 is modified
- PrintValueObj2 (); //The value of OBJ2 is printed here
- }
- modifyvalue{
- Value_ref2 = VALUE_REF1; //By value incoming reference value_ref1, get copy of Value_ref1
- Value_ref2->value_obj1.setvalue ("xxx"); //The value of Value_obj1 is modified
- PrintValueObj1 (); //The value of Obj1 is printed here
- }
It's clear enough! Value1_ref1 in the function as a parameter, the first copy is copied Value1_ref2 for the function domain use, at this time, these two ref all point to the same value_obj; the code in the NewObject function [value = new value ();] In fact, the VALUE1_REF1 points to a new object value_obj2; After that, the set operation is the operation of the new object, and the Modifyvalue function operates directly through the Set method Value_obj1, which is different from the NewValue function.
If you still do not understand, please first determine whether you have understood the "reference", "object" concept, you can Google, Baidu corresponding article study:)
How to pass Java function parameters