1 //the parameter passing mechanism of the method (1) and the basic type of the formal parameter are passed. 2 classprimitivetransfertest3 {4 Public Static voidSwapintAintb)5 {6 //The following code implements a and B switching7 inttemp =A;8A =b;9b =temp;TenSystem.out.println (the value of a in the swap method is: "+a+", the value of B is: "+b); One } A Public Static voidMain (string[] args) - { - intA = 6; the intB = 9; - Swap (A, b); -System.out.println ("After the swap is over, the value of variable A is:" +a+ ", and the value of variable B is:" +b); - } + } - + A //the parameter passing mechanism of the method (2); The reference type does the pass of the formal parameter. at classDatawrap - { - intA; - intb; - } - classreferencetransfertest in { - Public Static voidswap (Datawrap DW) to { + //The following code implements the exchange of A and B. -DW.A = dw.a^dw.b; thedw.b = dw.a^dw.b; *DW.A = dw.a^dw.b; $System.out.println (the value of a member variable in the Swap method is: "+dw.a+", the value of the B member variable is: "+dw.b);Panax NotoginsengSYSTEM.OUT.PRINTLN (DW);//[email protected] - //DW = null;//This is to prove that if the application of this DW is directed to NULL, it will not affect the point of the DW in the main method. the } + Public Static voidMain (string[] args) A { theDatawrap DW =Newdatawrap (); +DW.A = 6; -DW.B = 9; $ swap (DW); $System.out.println ("After the swap is over, the value of the a member variable is:" +dw.a+ ", the value of the B member variable is:" +dw.b); -SYSTEM.OUT.PRINTLN (DW);//[email protected]//It is proved that the address (that is, the pointer) is actually the value (site) passed by the reference pass. - } the}
As can be seen from the above two examples, there is only one form of parameter passing for a method in Java, that is, value passing.
There is only one way to pass the parameters of a method in Java: value passing