1. Java applications have and only one parameter passing mechanism, that is, passing by value
2, passing by value means that when a parameter is passed to a function, the function receives a copy of the original value
3. Passing by reference means that when a parameter is passed to a function, the function receives the memory address (copy) of the original value, not the copy of the value
Code:
Public classMain { Public Static voidMain (string[] args) {Employee worker=NewEmployee ("Zhang San"); Employee Manager=NewEmployee ("John Doe"); Swap (Worker,manager); System.out.println (Worker.name+" "+manager.name); }} classEmployee { PublicString name=NULL; PublicEmployee (String N) { This. name=N; } //exchange of two employee objects Public Static voidSwap (Employee E1,employee E2) {Employee Temp=E1; E1=E2; E2=temp; System.out.println (E1.name+" "+e2.name); }}
Results:
Lishizhang
View Code
Although the content of the Parameter object E1,e2 is exchanged, the worker,manager of the argument object does not have the interchange content. The most important reason for this is that the formal parameter e1,e2 is the address copy of the argument Worker,manager.
If you are in the JVM memory management of the Java stack of the local variable area, it is very good to understand the above statement. When the JVM runs the above program, running the main method and the swap method will successively push two memory spaces called stack frames in the Java stack.
The memory in the main stack frame called the local variable area is used to store references to the argument object worker and manager.
The local variable area in the swap stack frame stores references to parameter objects E1 and E2.
Although the reference values of E1 and E2 are the same as the worker and manager, they take up different memory space.
When the E1 and E2 references are exchanged, the following figure is clear that it does not affect the worker and manager reference values at all.
String,integer, double, etc. immutable types because they do not provide their own modified functions, each operation is a new object, so special treatment. Can be considered as a pass value.
Integer is the same as String. The class variable that holds the value is the final attribute, cannot be modified, and can only be re-assigned/generated new object.
When an integer is passed into the method as a method parameter, its assignment causes the reference of the original integer to be pointed to the stack address within the method and loses its point to the original class variable address. Any action done on the assigned integer object does not affect the original object.
Code:
Public classMain { 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 ()); System.out.println ("Before object exchange: P2 =" +p2.tostring ()); //exchanging P1 objects and P2 objectsPerson.swapobject (P1, p2); //as seen from the exchange results, the actual objects are not exchangedSystem.out.println ("\ n object after Exchange: P1 =" +p1.tostring ()); System.out.println ("After object exchange: P2 =" +p2.tostring ()); //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 (' \ n ' + ' object array before Exchange: arraya[0] = "+ arraya[0].tostring () +", arraya[1] = "+ arraya[1].tostring ()); System.out.println ("Object array before Exchange: arrayb[0] =" + arrayb[0].tostring () + ", arrayb[1] =" + arrayb[1].tostring ()); //swap these two object arraysPerson.swapobjectarray (Arraya, Arrayb); System.out.println (' \ n ' + ' object array after Exchange: arraya[0] = "+ arraya[0].tostring () +", arraya[1] = "+ arraya[1].tostring ()); System.out.println ("Object array after Exchange: arrayb[0] =" + arrayb[0].tostring () + ", arrayb[1] =" + arrayb[1].tostring ()); //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 (' \ n ' + ' base type array before Exchange: a[0] = "+ a[0] +", a[1] = "+ a[1]); System.out.println ("Base type array Exchange before: b[0] =" + b[0] + ", b[1] =" + b[1]); //swap two primitive type arraysPerson.swapintarray (A, b); System.out.println (' \ n ' + ' base type array after Exchange: a[0] = "+ a[0] +", a[1] = "+ a[1]); System.out.println ("Base type array after Exchange: b[0] =" + b[0] + ", b[1] =" + b[1]); //changing the contents of an object arrayPerson.changeobjectarray (Arraya, Arrayb); System.out.println (' \ n ' + ' object array contents exchanged and changed after: arraya[1] = "+ arraya[1].tostring ()); System.out.println ("Object array content exchanged and changed after: arrayb[1] =" + arrayb[1].tostring ()); //changing the contents of a primitive type arrayPerson.changeintarray (A, b); System.out.println (' \ n ' + ' base type array content Exchange and change after: a[1] = "+ a[1]); System.out.println ("Base type array content Exchange and change after: b[1] =" + b[1]); }}classPerson {PrivateString name; PrivateString sex; PublicPerson (string x, string y) { This. Name =x; This. Sex =y; } //overriding the ToString () method for easy output PublicString toString () {returnName + "" +sex; } //Interchange Object reference 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; }}
Results:
Before object exchange: P1 =Alexia Female Object Exchange before: P2=Edward Male after object exchange: P1=Alexia Female object after Exchange: P2=Edward Male Object array before swapping: arraya[0] = Alexia female, arraya[1] =Edward Male Object array before swapping: arrayb[0] = Jmwang female, arrayb[1] =HWU Male Object array after Exchange: arraya[0] = Alexia female, arraya[1] =Edward Male Object array after Exchange: arrayb[0] = Jmwang female, arrayb[1] =HWU male Basic type array before Exchange: a[0] = 0, a[1] = 1primitive Type array before Exchange: b[0] = 1, b[1] = 2base type array after Exchange: a[0] = 0, a[1] = 1base type array after Exchange: b[0] = 1, b[1] = 2Object array content Exchange and change after: arraya[1] =WJL Male Object array content Exchange and change: arrayb[1] =Edward male Basic type array content Exchange and change after: a[1] = 5base type array content Exchange and change after: b[1] = 1
View Code
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.
The above code, the array object person[],int[] to switch, will not change, but the p1[1],p2[1],x[1],y[1] to displace, you will find that can change the contents of arrays.
Http://www.jb51.net/article/80188.htm
http://blog.csdn.net/yangliuy/article/details/43230681
Http://www.cnblogs.com/lanxuezaipiao/p/3371224.html
Java Reference Delivery