Value passing and reference passing

Source: Internet
Author: User

Value passing: (formal parameter type is the basic data type): When the method is called, the actual parameter passes its value to the corresponding formal parameter, the formal parameter only initializes its own storage unit content with the value of the actual parameter, it is two different storage units, so the change of the formal parameter value in the method execution does not affect the value of the actual parameter.

Reference passing: (formal parameter type is a reference data type parameter): Also known as a pass-through address. Method invocation, the actual parameter is an object (or an array), then the actual parameters and formal parameters point to the same address, in the execution of the method, the operation of the formal parameters is actually the operation of the actual parameters, the result is preserved after the end of the method, so the change in the method execution parameters will affect the actual parameters.

 Packageedu.smc.xiao.test; Public classTest160107 { Public Static voidMain (string[] args) {A a=NewA ();        Change (A.name,a.hello); System.out.print (A.name+ "SAY:");  for(inti = 0; i < a.hello.length; i++) {System.out.print (a.hello[i]); } System.out.println ("."); }    Private Static voidChange (String name,Char[] Hello) {Name= "You"; hello[0] = ' W '; }    }classa{String name= "Java"; Char[] Hello = {' H ', ' e ', ' l ', ' l ', ' O '}; }
View Code

Value passing and reference 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.