pass by value and pass by reference in java

Java parameters, whether primitive or reference types, are passed a copy of the parameter.

If the parameter type is a primitive type, then the passed parameter is a copy of the parameter, that is, the value of the original parameter. 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 passed parameter is a copy of the reference parameter, and both references point to the same object value. If instead of changing the address of the copy in the function, the

The value of the address, then changes within the function will affect the parameters passed in. And if the address of the copy is changed in the function, such as new one, then the copy points to a new address, then the passed parameter still points to the original address,

All do not change the value of the parameter. And if the value of the copy is changed in the function first, and then the address of the copy is changed, such as new one, then the copy points to a new address, then the passed parameter still points to the original address,

But it will change the value of the parameter.

demo1:

 1 public class ValuePass {
 2 
 3          private static int num = 5;
 4 
 5          public static void changeValue(int n) {
 6 
 7                    n = n+2;
 8 
 9                    System.out.println("n="+n);
10 
11          }
12 
13          public static void main(String[] args) {
14 
15                    System.out.println("Before changeValue(), num="+ num);
16 
17                    changeValue(num);
18 
19                    System.out.println("After changeValue(), num="+ num);
20 
21          }
22 
23 }

demo2:

public class ReferencePass {    
    public static void changeSalary(Employ e, double raiseSalary) {
        e.setSalary (e.getSalary () + raiseSalary);
        System.out.println("e.salary="+ e.getSalary());
    }    
    public static void changeSalary2(Employ e, double raiseSalary) {
        e.setSalary (e.getSalary () + raiseSalary);
        e.setSalary (e.getSalary () +1000 );
        Employ ee = new Employ();
        ee.setSalary ( 20000 );
        e = ee ;
        System.out.println("e.salary="+ e.getSalary());
        System.out.println("ee.salary="+ ee.getSalary());
    }    
    public static void main(String[] args) {
        Employ e = new Employ();
        e.setSalary ( 10000 );
        System.out.println("Before method, e.getSalary()="+ e.getSalary());
        changeSalary(e, 1500);
        System.out.println("After method, e.getSalary()="+ e.getSalary()); 
        changeSalary2(e, 1500);
        System.out.println("After method, e.getSalary()="+ e.getSalary());
    }
}
class Employ {
    private double salary;
    public double getSalary() {
        return salary;
    }
    public void setSalary(double salary) {
        this.salary = salary;
    }    
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325302848&siteId=291194637