Java interview questions:
After an object is passed as a parameter to a method, this method can change the properties of this object and return the changed result. Is it a value transfer or a reference transfer?
Basically, the answer is: Pass the value
Note: The premise for this conclusion must be"The parameter value is a reference to the object, not the content of the object."
----------------------------------------------------
Value transfer:When a method is called, the actual parameter passes its value to the corresponding formal parameter.MediumThe change in execution form parameter value does not affect the actual parameter value.
Reference transfer:It is also called the transfer address. When the method is called, the actual parameter reference (that is, the address,Instead of parameter values) Is passed to the corresponding form parameter in the method,
In MethodMediumThe operation of the formal parameter is actually the operation of the actual parameter (address operation), so that the change of the formal parameter value in the method execution will affect the actual parameter value.
Value Transfer--Basic data type as a parameter(For example, String, INT)
Public static void changeint (int A, int B) {<br/> int TMP; <br/> TMP = A; <br/> A = B; <br/> B = TMP; <br/>}Parameter call:
// Int type, value transfer <br/> int A = 123; <br/> int B = 456; </P> <p> printint (A, B ); <br/> changeint (a, B); <br/> printint (A, B );Execution result:
A = 123; B = 456
A = 123; B = 456
----------------------------------------------------
Reference Transfer--Object or array as a parameter(For example, person, array)
<textarea readonly name="code" class="java">Class person {<br/> string name; <br/> int age; </P> <p> Public Person (string name, int age) {<br/> This. name = Name; <br/> This. age = age; <br/>}</P> <p> Public void changeperson (person, string name, int age) {<br/> person. name = Name; <br/> person. age = age; <br/>}</P> <p> Public void printperson (person) {<br/> system. out. println ("name =" + person. name + "; age =" + person. age); <br/>}< br/>}</textarea> Parameter call:
<textarea readonly name="code" class="java">// Object, passed by reference <br/> person P = new person ("Zhang San", 20); </P> <p> P. printperson (p); <br/> P. changeperson (P, "Li Si", 30); <br/> P. printperson (P );</textarea> Execution result:
Name = Zhang San; age = 20
Name = Li Si; age = 30
----------------------------------------------------
Complete example:
Package COM. val_ref; </P> <p> // person class <br/> class person {<br/> string name; <br/> int age; </P> <p> Public Person (string name, int age) {<br/> This. name = Name; <br/> This. age = age; <br/>}</P> <p> Public void changeperson (person, string name, int age) {<br/> person. name = Name; <br/> person. age = age; <br/>}</P> <p> Public void printperson (person) {<br/> system. out. println ("name =" + person. name + "; age =" + person. age); <br/>}</P> <p> class iarray {</P> <p> Public iarray () {</P> <p >}</P> <p> Public void reversearray (INT [] array) {<br/> int TMP; <br/> int Len = array. length; <br/> for (INT I = 0; I <Len/2; I ++) {<br/> TMP = array [I]; <br/> array [I] = array [len-1-i]; <br/> array [len-1-i] = TMP; <br/>}</P> <p> Public void printarray (INT [] array) {</P> <p> for (INT I = 0; I <array. length; I ++) {<br/> system. out. print (array [I] + ""); <br/>}< br/> system. out. println (); <br/>}</P> <p> public class testvalue {</P> <p> Public static void main (string [] ARGs) {</P> <p> // object, reference transfer <br/> person P = new person ("Zhang San", 20); </P> <p> P. printperson (p); <br/> P. changeperson (P, "Li Si", 30); <br/> P. printperson (p); </P> <p> // object, passed by reference <br/> int [] array = new int [5]; <br/> for (INT I = 0; I <array. length; I ++) {<br/> array [I] = I + 1; <br/>}< br/> iarray OBJ = new iarray (); <br/> obj. printarray (array); <br/> obj. reversearray (array); <br/> obj. printarray (array); </P> <p> // string type, value transfer <br/> string name1 = "zhangsan "; <br/> string name2 = ""; </P> <p> printstring (name1, name2); <br/> changestring (name1, name2 ); <br/> printstring (name1, name2); </P> <p> // int type, value transfer <br/> int A = 123; <br/> int B = 456; </P> <p> printint (a, B); <br/> changeint (A, B ); <br/> printint (a, B); <br/>}</P> <p> // string type <br/> Public static void changestring (string name1, string name2) {<br/> string tmpname; <br/> tmpname = name1; <br/> name1 = name2; <br/> name2 = tmpname; <br/>}</P> <p> Public static void printstring (string name1, string name2) {<br/> system. out. println ("name1 =" + name1 + "; name2 =" + name2 ); <br/>}</P> <p> // int type <br/> Public static void changeint (int A, int B) {<br/> int TMP; <br/> TMP = A; <br/> A = B; <br/> B = TMP; <br/>}</P> <p> Public static void printint (int A, int B) {<br/> system. out. println ("A =" + A + "; B =" + B); <br/>}< br/>}
Execution result:
Name = Zhang San; age = 20
Name = Li Si; age = 30
1 2 3 4 5
5 4 3 2 1
Name1 = Zhang San; name2 = Li Si
Name1 = Zhang San; name2 = Li Si
A = 123; B = 456
A = 123; B = 456
Recommendation reference:
Passing values and references in Java
Is Java passed by value or reference?
Whether to pass the value or reference when passing Java Parameters
Java = and equals comparison