Problems with the transfer of parameters
The argument of the reference type (in function call) is a rather ridiculous question. Some books say it is a value, and some books say it is a quote. The Java programmer is getting into a nervous division. So, let's finally talk about the issue of "reference type parameter passing."
As an example, let's say that we're going to print out the string of strings you just created, we'll use the following statement:
What does the new// statement mean, pinch? That's the two.
The first understanding: it can be thought that the input function is the str pointer, the pointer is plainly a value of an address, said a little more white, is an integer. According to this understanding, is the way to pass the value. In other words, the parameter passes the pointer itself, so it is passed value.
The second understanding: it can be thought that the StringBuffer object is passed in, according to this understanding, is the method of reference. Because we actually pass in the address of the object (that is, the reference).
So much saliva, in fact, whether it is a reference or a value, can make sense, the key depends on how you look at the parameters of the things passed. This is like the "wave-and-particle duality of light" in quantum mechanics, and if you measure it as a particle, it appears to be a particle, and if you are observing it in a fluctuating way, it looks like a wave.
/*** @description No reference pass in Java only value pass * *@authorAlexia * @date 2013-10-16 **/classPerson {PrivateString name; PrivateString sex; PublicPerson (string x, string y) { This. Name =x; This. Sex =fy; } //overriding the ToString () method for easy output PublicString toString () {returnName + "" +sex; } //Swap object references, where P1 and P2 are variables (local variables) that swapobject this function.//P1, p2 receive is passed in a heap memory address, change P1, p2 point just change the point of these two local variables. //The actual global variables are still unchanged. Public Static voidSwapobject (person P1, person p2) {person tmp=P1; P1=P2; P2=tmp; } //Exchange Base Type Public Static voidSwapint (intAintb) {intTMP =A; A=b; b=tmp; } //Swap Object Array Public Static voidSwapobjectarray (person[] p1, person[] p2) {person[] tmp=P1; P1=P2; P2=tmp; } //Exchange Primitive Type array Public Static voidSwapintarray (int[] x,int[] y) {int[] tmp =x; X=y; Y=tmp; } //Change the contents of an object array Public Static voidChangeobjectarray (person[] p1, person[] p2) {person tmp= P1[1]; p1[1] = p2[1]; p2[1] =tmp; //Modify the p1[1] againPerson p =NewPerson ("Wjl", "male"); p1[1] =p; } //Change the contents of a primitive type array Public Static voidChangeintarray (int[] x,int[] y) {intTMP = X[1]; x[1] = y[1]; y[1] =tmp; x[1] = 5; }} Public classByvaluetest { Public Static voidMain (string[] args) {//build and construct two objectsperson P1 =NewPerson ("Alexia", "female"); Person P2=NewPerson ("Edward", "male"); System.out.println ("Before object exchange: P1 =" + p1.tostring ());//alexia FemaleSystem.out.println ("Before object exchange: P2 =" + p2.tostring ());//edward Male //exchanging P1 objects and P2 objectsPerson.swapobject (P1, p2); //as seen from the exchange results, the actual objects are not exchangedSystem.out.println ("After object exchange: P1 =" + p1.tostring ());//alexia FemaleSystem.out.println ("After object exchange: P2 =" + p2.tostring ());//edward Male //create an array of two objectsperson[] Arraya =NewPerson[2]; Person[] Arrayb=NewPerson[2]; //construct an array object separatelyArraya[0] =NewPerson ("Alexia", "female"); arraya[1] =NewPerson ("Edward", "male"); arrayb[0] =NewPerson ("Jmwang", "female"); arrayb[1] =NewPerson ("HWU", "male"); System.out.println ("Object array before Exchange: arraya[0] =" + arraya[0].tostring () + ", arraya[1] ="
+ arraya[1].tostring ());//alexia Female,edward MaleSystem.out.println ("Object array before Exchange: arrayb[0] =" + arrayb[0].tostring () + ", arrayb[1] ="
+ arrayb[1].tostring ());//jmwang FEMALE,HWU Male //swap these two object arraysPerson.swapobjectarray (Arraya, Arrayb); System.out.println ("Object array after Exchange: arraya[0] =" + arraya[0].tostring () + ", arraya[1] ="
+ arraya[1].tostring ());//alexia Female,edward MaleSystem.out.println ("Object array after Exchange: arrayb[0] =" + arrayb[0].tostring () + ", arrayb[1] ="
+ arrayb[1].tostring ());//jmwang FEMALE,HWU Male //create two normal arrays int[] A =New int[2]; int[] B =New int[2]; //assigning values to an array of elements for(inti = 0; i < a.length; i++) {A[i]=i; B[i]= i + 1; } System.out.println ("Primitive type array before Exchange: a[0] =" + a[0] + ", a[1] =" + a[1]);//0,1System.out.println ("base type array before Exchange: b[0] =" + b[0] + ", b[1] =" + b[1]);//1,2 //swap two primitive type arraysPerson.swapintarray (A, b); System.out.println ("Base type array after Exchange: a[0] =" + a[0] + ", a[1] =" + a[1]);//0,1System.out.println ("base type array after Exchange: b[0] =" + b[0] + ", b[1] =" + b[1]);//1,2 //changing the contents of an object arrayPerson.changeobjectarray (Arraya, Arrayb); System.out.println ("Object array content exchanged and changed after: arraya[0] =" + arraya[0].tostring () + ", arraya[1] ="
+ arraya[1].tostring ());//alexia FEMALE,WJL MaleSystem.out.println ("Object array content exchanged and changed: arrayb[0] =" + arrayb[0].tostring () + ", arrayb[1] ="
+ arrayb[1].tostring ());//jmwang Female,edward Male //changing the contents of a primitive type arrayPerson.changeintarray (A, b); System.out.println ("Base type array content Exchange and change after: a[0] =" + a[0] + ", a[1] =" + a[1]);//0,5System.out.println ("base type array content Exchange and change after: b[0] =" + b[0] + ", b[1] =" + b[1]);//1,1 }}
Description: Whether it is an object, a primitive type, an array of objects, or an array of primitive types, the actual address cannot be changed in a function but can change its contents.
"equivalent to the value of the stack memory passed in, the stack memory only holds the address and the basic data content, is not in the function to change"
There is no reference pass in Java only the value is passed (in the function)