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