formal parameter

1. Formal parameters: used to receive the parameters passed when calling the method. The memory space is allocated only when it is called, and once the call ends, the memory space is freed. So it's only valid inside a method.

2. Argument: The value passed to the called method, created in advance and given a definite value.

3. Call by value: The parameters passed in the call by value are basic data types, and the parameters are regarded as formal parameters.

4. Call by reference: In a call by reference, if the passed parameter is a reference data type, the parameter is regarded as an actual parameter. In the process of calling, the address of the actual parameter is passed to the formal parameter, and the changes on the formal parameter all occur on the actual parameter.

1. Basic data type (call by value)



2. Reference data type (call by reference)
By reference, changing the reference of the formal parameter in the method body will not change the reference of the actual parameter, but it may change the attribute value of the actual parameter object.

Give two examples:

(1) The formal parameter reference is changed in the method body, but the actual parameter reference is not changed, and the actual parameter value remains unchanged.

  
public class TestFun2 {  
    public static void testStr(String str){  
    str="hello";//The type parameter points to the string "hello"  
    }  
    public static void main(String[] args) {  
    String s="1" ;  
    TestFun2.testStr(s);  
    System.out.println("s="+s); //The reference of the actual parameter s has not changed, and the value has not changed  
    }  
}

Execution result print: s=1

(2) In the body of the method, the content of the actual parameter object is changed by reference. Note that it is "content", and the reference is still unchanged.

   
public class TestFun4 {  
    public static void testStringBuffer(StringBuffer sb){  
    sb.append("java");//Change the content of the actual parameter  
    }  
    public static void main(String[] args) {  
    StringBuffer sb= new StringBuffer("my ");  
    new TestFun4().testStringBuffer(sb);  
    System.out.println("sb="+sb.toString());//The content has changed  
    }  
    }
 

Execution result, print: sb=my java.

So comparing the two examples where the parameters are String and StringBuffer will understand what "change the content of the actual parameter object" is.

Summary:

1.java's basic data type is call-by-value, and the object reference type is pass-by-reference.

2. When calling by value, the value of the formal parameter is changed, and the value of the actual parameter is not changed. The value of the actual parameter can be passed to the formal parameter, but this transfer is one-way, and the formal parameter cannot be passed back to the actual parameter .

3. When the reference is called, if the parameter is an object, no matter what operation is done to the object, the reference of the actual parameter object will not be changed, but if the content of the object is changed, the content of the actual parameter object will be changed.

  

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326596293&siteId=291194637