Home > Java > javaTutorial > body text

Detailed explanation of Java value passing and reference passing

高洛峰
Release: 2017-01-24 16:54:00
Original
1494 people have browsed it

When an object is passed as a parameter to a method, this method can change the properties of the object and return the changed results. So is it passed by value or by reference?
Answer: It is value transfer. The Java programming language has only value-passed parameters. When an object instance is passed as a parameter to a method, the parameter's value is a copy of the object's reference. Pointing to the same object, the contents of the object can be changed within the called method, but the reference to the object (not a copy of the reference) will never change.

Java parameters, whether they are primitive types or reference types, are passed by copies (there is another way of saying it is by value, but it is easier to understand by saying that it is by value. Passing by value is usually relative to passing by address) ).

If the parameter type is a primitive type, then what is passed is a copy of the parameter, which is the value of the original parameter. This is the same as the value passing discussed before. Changing the value of the copy within the function does not change the original value.

If the parameter type is a reference type, then what is passed is a copy of the reference parameter, 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 parameters passed in. If the address of the copy is changed in the function, such as new, then the copy points to a new address. At this time, the parameter passed in 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);
  }
 }
Copy after login

Display result:

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 //可见引用的副本进行了交换
End of method: y=Alice
After: a=Alice //引用本身没有交换
After: b=Bob
Copy after login

The above is the entire content of this article, I hope it will be useful for everyone’s learning Thank you for your help, and I hope everyone will visit the PHP Chinese website.

For more detailed articles on Java value passing and reference passing, please pay attention to the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!