When an object is passed as a parameter to a method, this method can change the properties of the object and return the changed result, is it a value pass or a reference pass?
A: The value is passed. The Java programming language has only value-passing parameters. When an object instance is passed as a parameter to a method, the value of the parameter is a copy of the object's reference. Point to the same object, the contents of the object can be changed in the called method, but the object's reference (not the referenced copy) is never changed.
Java parameters, whether primitive or reference, are passed as replicas (another argument is a pass value, but a copy is better understood, and the pass value is usually relative to the address).
If the parameter type is the original type, then a copy of the parameter is passed in, that is, the value of the original parameter, which is the same as the value that was previously discussed. If you change the value of a copy in a function, the original value is not changed. If the parameter type is a reference type, then a copy of the reference parameter is passed in, and the copy holds the address of the parameter. If you do not change the address of the copy in the function, but instead change the value in the address, the changes within the function will affect the parameters passed in. If you change the address of a copy in a function, such as new, the copy points to a new address, and the passed in parameter points to the original address, so the value of the parameter is not changed. Cases:
1 public class Paramtest {
2 public static void Main (string[] args) {
3/**
4 * Test 1:methods can ' t modify numeric parameters
5 */
6 System.out.println ("Testing triplevalue:");
7 Double percent = 10;
8 System.out.println ("before:percent=" + percent);
9 Triplevalue (percent);
Ten System.out.println ("after:percent=" + percent);
11
12/**
* Test 2:methods can change the state of object parameters
14 */
System.out.println ("\ntesting triplesalary:");
Employee Harry = new Employee ("Harry", 50000);
System.out.println ("before:salary=" + harry.getsalary ());
Triplesalary (Harry);
System.out.println ("after:salary=" + harry.getsalary ());
20
21/**
* Test 3:methods can ' t attach new objects to object parameters
23 */
System.out.println ("\ntesting swap:");
Employee A = new Employee ("Alice", 70000);
Employee B = New Employee ("Bob", 60000);
System.out.println ("before:a=" + a.getname ());
System.out.println ("before:b=" + b.getname ());
Swap (A, b);
System.out.println ("after:a=" + a.getname ());
System.out.println ("after:b=" + b.getname ());
32}
33
private static void Swap (employee x, employee y) {
Employee temp = x;
X=y;
Panax Notoginseng y=temp;
System.out.println ("End of method:x=" + x.getname ());
System.out.println ("End of method:y=" + y.getname ());
40}
41
private static void Triplesalary (Employee x) {
X.raisesalary (200);
System.out.println ("End of method:salary=" + x.getsalary ());
45}
46
$ private static void Triplevalue (Double x) {
X=3*x;
System.out.println ("End of Method x=" +x);
50}
51}
Show Results:
Testing Triplevalue:
before:percent=10.0
End of Method x= 30.0
after:percent=10.0
Testing Triplesalary:
before:salary=50000.0
End of method:salary=150000.0
after:salary=150000.0
Testing Swap:
Before:a=alice
Before:b=bob
End of Method:x=bob //A copy of the visible reference is exchanged
End of Method:y=alice
After:a=alice //reference itself is not exchanged
After:b=bob
Value passing and reference passing in Java