Elaborate on value passing, reference passing and address passing

Elaborate on value passing, reference passing and address passing

1 arguments and formal parameters

Formal parameter: the parameter used when defining the function name and function body. It is used to receive the parameters passed in when calling the function.

Actual parameters: when calling a parametric function, there is a data transfer relationship between the calling function and the called function. When calling a function in the main function, the parentheses in the name of the function are called "actual parameters".

func main() {
   i := sum(2, 4) //2 and 4 are arguments
   fmt.Println(i)
}

func sum(a int, b int) int { //a. B is a formal parameter
   c := a
   return b + c //a. B and C are formal parameters
}

2 value passing, reference passing and pointer passing:

Value Passing: refers to copying and passing a copy of the actual parameters to the function when calling the function, so that if the parameters are modified in the function, the actual parameters will not be affected.

Reference passing: refers to the direct transfer of the address of the actual parameter to the function when calling the function. The modification of the parameter in the function will affect the actual parameter.

Pointer passing: refers to directly passing the pointer address of the actual parameter to the function when calling the function, which is similar to reference passing. The biggest difference is that the type pointed to by the pointer cannot be null.

The difference between pointer passing and reference passing: pointer is address variable, so reference is equivalent to taking address constant as parameter, and pointer is equivalent to taking address variable as parameter.

2.1 Go language demonstration

type Obj struct {
   Id   int
   Name string
}

func main() {
	//Value passing and reference passing tests
	fmt.Println("pass by value...")
	v1 := 2
	v2 := Obj{2, "ls"}
	fmt.Println(v1, v2)
	updateVal(v1, v2)
	fmt.Println(v1, v2)

	//Pointer passing test
	fmt.Println("Pointer passing...")
	v3 := 2
	v4 := Obj{2, "ls"}
	fmt.Println(v3, v4)
	updateValPoint(&v3, &v4)
	fmt.Println(v3, v4)
}

//pass by value
func updateVal(v1 int, v2 Obj) {
   v1 = 1
   v2 = Obj{1, "zs"}
}

//Reference passing
func updateValPoint(v1 *int, v2 *Obj) {
   *v1 = 12
   *v2 = Obj{1, "zs"}
}

Output:

pass by value...
2 {2 ls}
2 {2 ls}
Pointer passing...
2 {2 ls}
12 {1 zs}

2.2 Java language demonstration

public static void main(String[] args) {
    System.out.println("Value transfer 1");
    int v1 = 2;
    String v2 = "zz";
    Obj v3 = new Obj(2, "ls");
    System.out.println(v1 + "--" + v2 + "--" + v3);
    updateValue(v1, v2, v3);
    System.out.println(v1 + "--" + v2 + "--" + v3);

    System.out.println("Value transfer 2");
    int v4 = 2;
    String v5 = "zz";
    Obj v6 = new Obj(2, "ls");
    System.out.println(v4 + "--" + v5 + "--" + v6);
    updateValuePoint(v4, v5, v6);
    System.out.println(v4 + "--" + v5 + "--" + v6);
}

public static void updateValue(int v1, String v2, Obj v3) {
    v1 = 1;
    v2 = "ww";
    v3 = new Obj(1, "zs");
    v3.setId(1);
    v3.setName("zs");
}

public static void updateValuePoint(int v1, String v2, Obj v3) {
    v1 = 1;
    v2 = "ww";
    v3.setId(1);
    v3.setName("zs");
}

@AllArgsConstructor
@ToString
@Data
static class Obj {
    public int id;
    public String name;
}

Output:

2--zz--Main.Obj(id=2, name=ls)
2--zz--Main.Obj(id=2, name=ls)
2--zz--Main.Obj(id=2, name=ls)
2--zz--Main.Obj(id=1, name=zs)

3 Summary

In Java methods, the parameter list has two types of parameters, basic type and reference type.

Basic type: the value is stored in the local variable table. Any modification will only modify the value of the current stack frame. After the method is executed, there will be no change outside the method; At this time, you need to change the variables of the outer layer, and you must return to the active assignment.

Reference data type: pointer is stored in a local variable table. When invoking method, the replica refers to the stack, and assignment only changes the reference of the copy. However, if the object referencing the address is modified by manipulating the value referenced by the copy, the object referencing the address other than the method will be modified. (two references, the same address, and any modification will take effect at the same time.).

Both types copy a copy of the external parameter variable into the local variable. The basic type is value copy, and the reference type is to copy a copy of the reference address.

Reference articles

https://www.cnblogs.com/lingyejun/p/11028808.html

https://blog.csdn.net/belongtocode/article/details/107713117

Keywords: Programming

Added by leon77 on Sun, 30 Jan 2022 18:17:17 +0200