What is value transfer? What is reference transfer?
A value is a copy of the value to be passed. For example
Int I = 4;
Int J = I;
Here, a copy of 14 is copied to J, and the result is I = 4, j = 4.
Reference transfer: transmits the memory address of the referenced object.
Int * P, * P1;
Int J = 5; * P = J; // here, the address of J in the memory is assigned to the pointer P
P1 = P; // The address saved in Pointer P (the address of J) is assigned to pointer P1.
J = 4; // here, the output result of changing the J value is J = 4, * P = 4, * P1 = 4 // because P and P1 point to J, their values change with J.
(The above description and code come from the http://zhidao.baidu.com/question/31191252.html)
In Java, does function parameters pass through values or references?
The parameters passed to the function in Java are copies of the original variables. For non-class types such as int and float, this copy is naturally out of relationship with the original variables, this is not hard to understand;
For an instance of the class, this copy happens to be a copy of the Instance reference. The instance address pointed to by the instance (parameter) is the same as the instance address pointed to by the original Instance reference, therefore, modifications to parameters sometimes affect the real
The example itself causes the illusion that the Java transfer class instance is the passed reference, that is, the address. For example, the changememberfield function in the following example, but we change the address pointed to by the parameter
In other instances, for example, the changember function, we will find that the parameter and the original Instance reference will no longer have a relationship. Here, it is proved that the parameter passing in Java is completely passing the value rather than the address.
Sample Code:
Code:
Package
Com. sitinspring;
Public
Class
Changeclass {
Public
Void
Changeint (
Int
I ){
I
=
5
;
}
Public
Void
Changelong (
Long
I ){
I
=
555
;
}
Public
Void
Changestring (string Str ){
Str
=
"
5555
"
;
}
Public
Void
Changemember (Member member ){
Member
=
New
Member (
"
Cindy
"
,
35
);
}
Public
Void
Changememberfield (Member member ){
Member. setage (
20
);
Member. setname (
"
Andy
"
);
}
Public
Static
Void
Main (string [] ARGs ){
Changeclass
=
New
Changeclass ();
Int
Intvalue
=
10
;
Changeclass. changeint (intvalue );
System. Out. println (intvalue );
Long
Longvalue
=
10
;
Changeclass. changelong (longvalue );
System. Out. println (longvalue );
String stringvalue
=
"
1234
"
;
Changeclass. changestring (stringvalue );
System. Out. println (stringvalue );
Member member2
=
New
Member (
"
Douglas
"
,
45
);
Changeclass. changemember (member2 );
System. Out. println (member2 );
Member
=
New
Member (
"
Bill
"
,
25
);
Changeclass. changememberfield (member );
System. Out. println (member );
}
}
Package
Com. sitinspring;
Public
Class
Member {
Private
String name;
Private
Int
Age;
Public
Member (string name,
Int
Age ){
This
. Age
=
Age;
This
. Name
=
Name;
}
Public
String tostring (){
Return
"
Member name =
"
+
Name
+
"
Age =
"
+
Age;
}
Public
Int
Getage (){
Return
Age;
}
Public
Void
Setage (
Int
Age ){
This
. Age
=
Age;
}
Public
String getname (){
Return
Name;
}
Public
Void
Setname (string name ){
This
. Name
=
Name;
}
}
Output
:
10
10
1234
Member name
=
Douglas age
=
45
Member name
=
Andy age
=
20
Explanation
:
The first output 10 is because int is the basic type and the passed parameter is the copy of intvalue. The modification to the copy has no effect on the original intvalue.
The first output 10 is the same as the above.
The third output is 1234. Because string is of the class type,
STR is the copy of the stringvalue address. The STR parameter points to the same address as the stringvalue, but in the changestring Function
In, because of the special nature of string, STR = "5555" and STR = new string ("5555") are equivalent,
STR points to the address of the new "5555". After this sentence, STR is completely out of contact with the original stringvalue.
Fourth output member? Name = Douglas? Age = 45 is the same as above.
Fifth output member? Name = Andy? Age = 20 because the Member parameter is modified in the changememberfield Function
The value, that is, modifying the value that Member points to the instance, and this instance is the value that Member points to, so Member is changed to name = Andy and age = 20.
Conclusion
In Java, all parameters are passed with values corresponding to the basic type, passing copies of the original values; for class types, passing is a copy of the reference, that is, the address.
Whether changes to parameters of a function affect the original value. The value type naturally does not affect the original value. for the class type, it depends on whether the parameter address or value is changed. If it is the former, the parameters and the original reference point to different addresses, and they are naturally out of the relationship; if it is the latter, the modified instance is the instance pointed to by the original reference, which of course modifies the original value.
This article cited from: http://www.blogjava.net/junglesong/archive/2008/02/22/181246.html