Reference: http://blog.csdn.net/weidan1121/
1. The reference is as follows:
Objects will never be passed in Java applications, but only object references will be passed. Therefore, objects are passed by reference. However, it is important to distinguish how parameters are transmitted. This is the intent of the excerpt. The fact that Java applications pass objects by reference does not mean that Java applications PASS Parameters by reference. Parameters can be object references, while Java applications pass object references by value. Variables in Java applications can be of one of the following two types: reference type or basic type. When passed as a parameter to a method, the two types of processing methods are the same. Both types are passed by value; none are passed 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 this 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 rather than the copy of the value. Therefore, if the function modifies this parameter, the original value in the call code also changes. When the parameter passed to the function is not a reference, it is a copy of the value (transmitted by value ). The difference is reference. In C ++, when the parameter passed to the function is a reference, you pass the reference or memory address (by reference ). In Java applications, when an object reference is a parameter passed to a method, you pass a copy of the reference (passed by value) instead of the reference itself. Java applications pass all parameters by value, so that a copy of all parameters is made regardless of their type.
- Class Test
- {
- Public static void main (string ARGs [])
- {
- Int val;
- Stringbuffer SB1, sb2;
- Val = 10;
- SB1 = new stringbuffer ("Apples ");
- Sb2 = new stringbuffer ("Pears ");
- System. Out. println ("Val is" + val );
- System. Out. println ("SB1 is" + SB1 );
- System. Out. println ("sb2 is" + sb2 );
- System. Out. println ("");
- System. Out. println ("calling modify ");
- //Pass all parameters by value
- Modify (Val, SB1, sb2 );
- System. Out. println ("returned from modify ");
- System. Out. println ("");
- System. Out. println ("Val is" + val );
- System. Out. println ("SB1 is" + SB1 );
- System. Out. println ("sb2 is" + sb2 );
- }
- Public static void modify (int A, stringbuffer R1,
- Stringbuffer R2)
- {
- System. Out. println ("in modify ...");
- A = 0;
- R1 = NULL;// 1
- R2.append ("taste good ");
- System. Out. println ("A is" + );
- System. Out. println ("R1 is" + R1 );
- System. Out. println ("R2 is" + R2 );
- }
- }
Java application output
- Val is 10
- SB1 is apples
- Sb2 is pears
- Calling modify
- In modify...
- A is 0
- R1 is null
- R2 is pears taste good
- Returned from modify
- Val is 10
- SB1 is apples
- Sb2 is pears taste good
This Code declares three variables: an integer variable and two object references. Set the initial values of each variable and print them out. Then, all three variables are passed as parameters to the modify method.
The modify method changes the values of all three parameters:
Set the first parameter (integer) to 0.
Set the first object to reference R1 to null.
Retain the second value that references R2, but change the object it references by calling the append method (similar to the processing of pointer P in the preceding C ++ example ).
When the execution returns to main, the values of the three parameters are printed again. As expected, the Val of the integer type has not changed. The reference to SB1 does not change either. If SB1 is passed by reference, as many people claim, it will be null. However, because the Java programming language passes all parameters by value, a copy of the reference of SB1 is passed to the modify method. When the modify method sets R1 to null at the position of // 1, it only performs this operation on a copy referenced by SB1, instead of operating the original value as in C ++.
Note that the second object references sb2 to print the new string set in the modify method. Even if the variable R2 in modify only references a copy of sb2, they point to the same object. Therefore, the method called for copying references changes the same object.
2. first understand the phrase "only passing values in Java ".
When a variable is passed, the value of the variable is received in the function. When an object reference is passed, the function receives a copy referenced by this object (note that it is a copy)
3. Next, I would like to add a description of the above program.
① The val of an integer is not changed: because the value of the variable is passed.
② The SB1 object reference has not changed. If SB1 is passed by reference, as many people claim, it will be null. However, because the Java programming language passes all parameters by value, a copy of the reference of SB1 is passed to the modify method. When the modify method sets R1 to null at the position of // 1, it only performs this operation on a copy referenced by SB1, instead of operating the original value as in C ++. : Only assign values to the referenced copy.
③ Note that the second object references sb2 to print the new string set in the modify method. Even if the variable R2 in modify only references a copy of sb2, they point to the same object. Therefore, the method called for copying references changes the same object. : The referenced function is called. Both the referenced copy and Reference refer to the same object.