For the basic type of delivery, we are easy to understand, and for the object, always feel is to pass by reference, see the following program:
public class ObjectRef {//basic type parameter passing
public static void Testbasictype (int m) {
System.out.println ("m=" + m);//m=50
m = 100;
System.out.println ("m=" + m);//m=100
}
parameter is an object, does not change the value of the reference??????
public static void Add(StringBuffer s) {//Changemethod
S.append ("_add");
}
Inside the test class:
————————————————————————————————————————————
StringBuffer Smain = new StringBuffer ("Init");
System.out.println ("smain=" + smain.tostring ());//smain=init
Add (Smain);
System.out.println ("smain=" + smain.tostring ());//smain=init_add
Passing in a reference variable, equivalent to a reference pass in C + +, is changed inside the called method, and the outside changes accordingly.
Changeref (Smain);
System.out.println ("smain=" + smain.tostring ());//smain=init_add
——————————————————————————————————————————————————
parameter is the object, change the value of the reference?????
public static void Changeref(StringBuffer s) {//Changeref method
s = new StringBuffer ("Java");
}
public static void Main (string[] args) {
int i = 50;
Testbasictype (i);
System.out.println (i);//i=50
StringBuffer smain = new StringBuffer ("Init");
System.out.println ("smain=" + smain.tostring ());//smain=init
Add (smain);
System.out.println ("smain=" + smain.tostring ());//smain=init_add
Changeref (Smain);
System.out.println ("smain=" + smain.tostring ());//smain=init_add
}
}
The allowed results of the above program show that the parameters of the Testbasictype method are basic types, although the value of the parameter m changes, but does not affect I.
The argument to the Add method is an object, and when the Smain is passed to the parameter s , S gets a copy of the Smain, so s and Smain point to the same object, so the use of the S operation is actually the object that the Smain points to, so after calling the Add method, The content of the object pointed to by Smain has changed. (ok Good)
In the Changeref method, the parameter is also an object, and when the Smain is passed to the parameter S, S gets a copy of the Smain, but unlike the Add method, the object that s points to is changed in the method body ( that is, S pointing at other objects , holding the balloon's rope for the balloon ),
Give s after the value is re-assigned, s with the Smain has no connection , ( Two has a different point) it and Smain point to different objects , so no matter what the operation on S, does not affect the object that Smain points to, Therefore, the object content that the Smain points to before and after calling the Changeref method has not changed .
(Above the essence of the essence of the sentence)
For the result of the call to the Add method, many people may have this feeling: Is it not obvious that it is passed by reference? For this kind of question, or to apply Bruce Eckel: It depends on how you look at the citation, and eventually you will understand that the argument is not so important. What really matters is that you understand that passing references makes changes to (the caller's) objects unpredictable.
Internship the next day-java parameter passing-essence in the last 2 words of the article