There is only one way to pass the parameters of a method in Java: value passing

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.