A call to a method parameter is a value call instead of a reference call

Source: Internet
Author: User

Tag: class parameter Reference value call reference call

A call to a method parameter is a value call instead of a reference call




The call to the package com.ray.object;/** * Method parameter is a value call, not a reference call *  * @author Ray * @since 2015-04-22 * @version 1.0 *  */public CLA SS Person {private static void swap (person A, person B) {Person temp = A;a = b; System.out.println ("A:" + a); b = temp; System.out.println ("B:" + b);} public static void Main (string[] args) throws Exception {person bill = new person (); Person Jack = new person (); System.out.println ("--bill:" + Bill); System.out.println ("**jack:" + Jack); Person.swap (bill, Jack); System.out.println ("--bill:" + Bill); System.out.println ("**jack:" + Jack);}}

Output:

--bill:[email protected]
**jack:[email protected]
A:[email protected]
B:[email protected]
--bill:[email protected]
**jack:[email protected]


From the above output can be seen, in the swap method, two parameters are indeed swapped, but because the parameter is an object, all calls are value calls, not reference calls,

Only the value returned by the parameter has changed, but the corresponding object has not changed, so the final result two objects are not swapped over


A call to a method parameter is a value call instead of a reference call

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.