java- function parameter passing

Function parameter passing

Function embodiment, there are two transmission parameters

Parameter passing by value, the function receives the value provided by the caller

Pass-by-reference parameter, the function receives the variable address supplied by the caller

 

Function parameter passing process

So in what way is the java parameter passing it? The answer is that by using the value of parameter passing, a function of shape parameter is obtained a copy of the argument values.

Transmission parameters for basic data types

 1 public class Test {
 2     public static void doubleValue(int num) {
 3         num = num * 2;
 4     }
 5     
 6     public static void main(String[] args) {
 7         int x = 1;
 8         doubleValue(x);
 9         System.out.println(x); //1
10     }
11 }

 

Implementation process

(1) call doubleValue (x), num copy is initialized to a value of x, i.e., num = 1;

(2) function in vivo, after NUM equals 2 multiplied by 2, where x is 1;

After completion (3) function is executed, num variable is no longer used.

 

 Parameter reference type is passed

 1 package ja.see.models;
 2 
 3 class Employee {
 4     private double salary;
 5     
 6     public Employee(double salary) {
 7         this.salary = salary;
 8     }
 9     
10     public double getSalary() {
11         return salary;
12     }
13 
14     public void setSalary(double salary) {
15         this.salary = salary;
16     }
17 
18     public void doubleSalary(double salary) {
19         this.salary = 2 * salary;
20     }
21 }
22 
23 public class Test {
24     public static void increaseSalary(Employee e) {
25         e.doubleSalary(e.getSalary());
26     }
27     
28     public static void main(String[] args) {
29         Employee e1 = new Employee(3000.00);
30         System.out.println("After method: " + e1.getSalary());
31         increaseSalary(e1);
32         System.out.println("After method: " + e1.getSalary());
33     }
34 }

 

Implementation process

Copy (1) when the function execution increaseSalary, e is initialized to a value that is copied e1 Employee object reference, e and e1 reference the same object

(2) function in vivo, a reference to the operation object, the object reference salary e and e1 is multiplied by 2 at the same time

After completion (3) function is executed, the variable e is no longer used, the variable e1 continued reference pay be modified example of the Employee object twice

 

For example evidence to the contrary

The chestnut above, if built on the java function parameter passing is a reference parameter passing point of view is also true, so let's take an example to prove java function parameter passing by value parameter passing.

package com.java.se;

class Employee {
    private String name;
    
    public Employee(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

public class Test {
    public static void sayName(Employee e) {
        e = new Employee("tom");
    }
    
    public static void main(String[] args) {
        Employee e1 = new Employee("lily");
        System.out.println("Before method: " + e1.getName()); //lily
        sayName(e1);
        System.out.println("After method: " + e1.getName()); //lily
    }
}

 

If the java function parameters are passed by reference, then After Method after e1.getName () should not be tom Lily;

 

to sum up

(1) a basic function can not modify a data argument type (numeric, and boolean)

(2) a function to change the status of an object argument

(3) a function can not make a reference to the object argument to a new object

 

Reference: "JAVA core technology"

Guess you like

Origin www.cnblogs.com/marton/p/11253250.html