2008-07-13java FAQ series (5) -- passed the value or passed the reference?
Keywords: Java interview question value transfer reference TransferAuthor: zookeeper (zangweiren)
Web: http://zangweiren.javaeye.com
>>>Reprinted please indicate the source!<
Are values transmitted in Java? Is there any reference transfer?
Before answering these two questions, let's take a look at the Code:
Java code
- Public class paramtest {
- // The initial value is 0.
- Protected int num = 0;
- // Assign a value to the method parameter
- Public void change (int I ){
- I = 5;
- }
- // Assign a value to the method parameter
- Public void change (paramtest t ){
- Paramtest TMP = new paramtest ();
- TMP. num = 9;
- T = TMP;
- }
- // Change the value of the method parameter
- Public void add (int I ){
- I + = 10;
- }
- // Change the value of the method parameter attribute
- Public void add (paramtest pt ){
- PT. Num + = 20;
- }
- Public static void main (string [] ARGs ){
- Paramtest T = new paramtest ();
- System. Out. println ("parameter -- Basic Type ");
- System. Out. println ("Original Value:" + T. Num );
- // Assign a value for the basic type parameter
- T. Change (T. Num );
- System. Out. println ("after assignment:" + T. Num );
- // Assign a value to the referenced Parameter
- T. Change (t );
- System. Out. println ("after calculation:" + T. Num );
- System. Out. println ();
- T = new paramtest ();
- System. Out. println ("parameter -- reference type ");
- System. Out. println ("Original Value:" + T. Num );
- // Change the value of the basic type parameter
- T. Add (T. Num );
- System. Out. println ("after being referenced:" + T. Num );
- // Change the property value of the object to which the reference type parameter points
- T. Add (t );
- System. Out. println ("after modifying the attribute:" + T. Num );
- }
- }
Public class paramtest {// The initial value is 0 protected int num = 0; // The Public void change (int I) {I = 5;} is assigned to the method parameter again ;} // assign public void change (paramtest t) {paramtest TMP = new paramtest (); TMP to the method parameter. num = 9; t = TMP;} // change the value of the method parameter public void add (int I) {I + = 10 ;} // change the public void add (paramtest pt) {pt. num + = 20;} public static void main (string [] ARGs) {paramtest T = new paramtest (); system. out. println ("parameter -- Basic Type"); system. out. println ("Original Value:" + T. num); // re-assign t for the basic type parameter. change (T. num); system. out. println ("after assignment:" + T. num); // value t for the referenced parameter. change (t); system. out. println ("after calculation:" + T. num); system. out. println (); t = new paramtest (); system. out. println ("parameter -- reference type"); system. out. println ("Original Value:" + T. num); // change the value of the basic type parameter T. add (T. num); system. out. println ("after being referenced:" + T. num); // change the attribute value T of the object to which the reference type parameter points. add (t); system. out. println ("after modifying attributes:" + T. num );}}
The running result of this Code is as follows:
- Parameter -- Basic Type
- Original Value: 0
- After Value assignment: 0
- After calculation: 0
- Parameter -- reference type
- Original Value: 0
- After being referenced: 0
- After modifying attributes: 20
From the above intuitive results, we can easily draw the following conclusions:
- For the basic type, the method parameter is assigned a value again in the method body, and the value of the original variable is not changed.
- For the reference type, the method parameter is re-referenced in the method body, and does not change the reference of the original variable.
- In the method body, parameters are calculated without affecting the value of the original variable.
- In the method body, operations are performed on the attributes of the object to which the parameters point. The attribute values of the objects to which the original variables point are changed.
What we have summarized above is what we see on the surface. So why does this happen? This requires the concept of value transfer and reference transfer. This issue has always been quite controversial.
As we all know, there are two types of variation in Java:
- Basic type variables, including char, byte, short, Int, long, float, double, and Boolean.
- Reference type variables, including classes, interfaces, and arrays (basic type arrays and object arrays ).
When a variable of the basic type is passed as a parameter to a method, the Java Virtual Machine copies the value and then transmits the copied value to the method. So in the above example, let's look back at this method:
Java code
- // Assign a value to the method parameter
- Public void change (int I ){
- I = 5;
- }
// Assign public void change (int I) {I = 5;} to the method parameter again ;}
When this method is called, the attribute num of variable I and paramtest object T has the same value, but it is two different variables. Variable I is the scope created by the Java Virtual Machine in
The local variable in the change (int I) method ends after the method is executed. In Java virtual machines, they are stored in a similar way:
Obviously, when the basic type is passed as a parameter to the mode, it is a value transfer, and the reference concept is not involved in the whole process. This is also widely accepted. This is also true for Boolean variables. See the following example:
Java code
- Public class booleantest {
- // Boolean Value
- Boolean bool = true;
- // Assign a value to a Boolean Parameter
- Public void change (Boolean B ){
- B = false;
- }
- // Calculate Boolean Parameters
- Public void calculate (Boolean B ){
- B = B & false;
- // Output the calculation result for convenience of comparison
- System. Out. println ("value after B operation:" + B );
- }
- Public static void main (string [] ARGs ){
- Booleantest T = new booleantest ();
- System. Out. println ("parameter -- Boolean ");
- System. Out. println ("Original Value:" + T. bool );
- // Assign a value to a Boolean Parameter
- T. Change (T. bool );
- System. Out. println ("after assignment:" + T. bool );
- // Change the value of the Boolean Parameter
- T. Calculate (T. bool );
- System. Out. println ("after calculation:" + T. bool );
- }
- }
Public class booleantest {// Boolean bool = true; // the value of public void change (Boolean B) {B = false for the Boolean parameter ;} // calculate the public void calculate (Boolean B) {B = B & false; // output the calculation result to system for convenience of comparison. out. println ("value after B operation:" + B);} public static void main (string [] ARGs) {booleantest T = new booleantest (); system. out. println ("parameter -- Boolean"); system. out. println ("Original Value:" + T. bool); // re-assign t for the Boolean parameter. change (T. bool); system. out. println ("after assignment:" + T. bool); // change the value of the Boolean parameter T. calculate (T. bool); system. out. println ("after calculation:" + T. bool );}}
The output result is as follows:
- Parameter-Boolean
- Original Value: True
- True
- Value After B operation: false
- After calculation: True
How does the Java Virtual Machine Process referenced variables as parameters when they are passed to the method? Similarly, it will copy a reference held by this variable and pass it to Java virtual
The local variable created by the machine for the method, and the two variables point to the same object. In the example at the beginning of the article, the paramtest type variables T and local variables PT in the Java Virtual Machine are as follows:
Storage:
There is a saying that when an object or a reference type variable is passed as a parameter, it is also a value transfer. This value is the reference of the object. Therefore, only the value is passed in Java and no reference is passed. There is also
Reference can be regarded as the alias of an object. when an object is passed as a parameter to a method, the object is referenced and therefore passed as a reference. These two views have their own supporters, but the previous view is largely
People accept this, including the author of core Java and the creator of Java, James Gosling, and thinking in
Bruce Eckel, author of Java, stands on a neutral standpoint.
In my opinion, the value in the value transfer refers to a value of the basic type. Even if the boolean type is expressed as true or false, in the stack, it is still saved as a numerical value.
0 indicates false, and other values indicate true. Reference is a tool used to operate objects. It contains information about the address of objects stored in the heap. Even if it is passed to the method as a parameter
It is a copy, but it is still referenced. Therefore, it is more clear in concept that the difference and value transfer are implemented through reference transfer.
Finally, we come to the following conclusion:
- When the basic type and basic type variables are passed as parameters to the method, they are passed as values. In a method object, you cannot assign a value to the original variable or change its value.
- When an object or referenced variable is passed as a parameter to a method, the original variable cannot be assigned a value in the method object, but the attributes of the object to which it points can be changed. It does not matter whether it is a value transfer or a reference transfer. It is important to know what will happen in the method body when a reference is passed as a parameter to a method.