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. So is it a value pass or a reference pass here?
Answer: It is value transfer. The Java programming language only passes parameters with values. When an object instance is passed into a method as a parameter, the value of the parameter is a copy of the reference of the object. Pointing to the same object, the content of the object can be changed in the called method, but the object's reference (not a copy of the reference) will never change.
Java parameters, whether they are primitive types or reference types, pass all copies (there is another saying is to pass values, but it is better to understand that passing copies, and passing values is usually relative to passing addresses).
If the parameter type is a primitive type, then the passed is a copy of this parameter, that is, the value of the original parameter, which is the same as the passed value mentioned before. If the value of the copy is changed in the function, the original value will not be changed.
If the parameter type is a reference type, then the copy of this reference parameter is passed over, and this copy stores the address of the parameter. If the address of this copy is not changed in the function, but the value in the address is changed, then the change within the function will affect the passed parameters. If the address of the copy is changed in the function, such as new one, then the copy points to a new address. At this time, the incoming parameter still points to the original address, so the value of the parameter will not be changed.
example:
public class ParamTest { public static void main(String[] args){ /** * Test 1: Methods can't modify numeric parameters */ System.out.println("Testing tripleValue:"); double percent = 10; System.out.println("Before: percent=" + percent); tripleValue(percent); System.out.println("After: percent=" + percent); /** * Test 2: Methods can change the state of object parameters */ 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()); /** * Test 3: Methods can't attach new objects to object parameters */ 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()); } private static void swap(Employee x, Employee y) { Employee temp = x; x=y; y=temp; System.out.println("End of method: x=" + x.getName()); System.out.println("End of method: y=" + y.getName()); } private static void tripleSalary(Employee x) { x.raiseSalary(200); System.out.println("End of method: salary=" + x.getSalary()); } private static void tripleValue(double x) { x=3*x; System.out.println("End of Method X= "+x); } }Show results:
Testing tripleValue:Before: percent=10.0End of Method X= 30.0After: percent=10.0Testing tripleSalary:Before: salary=50000.0End of method: salary=150000.0After: salary=150000.0Testing swap:Before: a=AliceBefore: b=BobEnd of method: x=Bob //The copy of the visible reference is swapped End of method: y=AliceAfter: a=Alice //The reference itself is not swapped After: b=Bob
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.