Write good, original post address: http://topic.csdn.net/u/20080713/17/ba211d6e-cb22-4274-ab80-dea990783f22.html
Author: zookeeper (zangweiren)
Web: http://zangweiren.javaeye.com
>>> Reprinted, please specify 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 final 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 ();
// Assign a value for the basic type parameter
T. Change (T. Num );
System. Out. println (T. Num );
// Assign a value to the referenced Parameter
T. Change (t );
System. Out. println (T. Num );
// Change the value of the basic type parameter
T. Add (T. Num );
System. Out. println (T. Num );
// Change the property value of the object to which the reference type parameter points
T. Add (t );
System. Out. println (T. Num );
}
}
The running result of this Code is as follows:
0
0
0
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. Therefore, in the preceding example
Java code
// Assign a value to the method parameter
Public void change (int I ){
I = 5;
}
In the case of method, the attribute num of variable I and paramtest object T has the same value, but it is two different variables. Variable I is a local variable created by the Java Virtual Machine in the change (int I) method. After the method is executed, its lifecycle ends. In Java virtual machines, they are stored in a similar way:
[Img] Quit
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 final 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 & = false;
// Output the calculation result for convenience of comparison
System. Out. println ("calculated value:" + B );
}
Public static void main (string [] ARGs ){
Booleantest T = new booleantest ();
// Assign a value to a Boolean Parameter
T. Change (T. bool );
System. Out. println (T. bool );
// Change the value of the Boolean Parameter
T. Calculate (T. bool );
System. Out. println (T. bool );
}
}
The output result is as follows:
True
Value after calculation: false
True
How does the Java Virtual Machine Process referenced variables as parameters when they are passed to the method? Similarly, it copies a reference held by the variable and passes it to the local variable created for the method by the Java Virtual Machine, so that the two variables point to the same object. In the example given at the beginning, the paramtest type variables T and local variables PT are stored in the Java Virtual Machine in the following way:
[Img] Quit
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. This statement obfuscated the concept of value and reference.
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, it is still saved as a value in the stack, 0 indicates true, and other values indicate false. Reference is a tool used to operate objects. It contains information about the address of objects stored in the heap. Even when it is passed as a parameter to a method, it is actually a copy of it, but it is still referenced.
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 a referenced variable is passed as a parameter to a method, it is passed as a reference. In a method object, you cannot assign a value to the original variable, but you can change the attributes of the object it points.