How to pass method parameters in Java

    There are two ways to pass parameters to methods. Pass-by-value means that the method accepts the value provided by the caller; call-by-reference means that the method accepts the variable address provided by the caller. Note: 1. A method cannot modify a parameter of a basic data type (that is, a numeric type or boolean). 2. A method can change the state of an object (array) parameter. 3. A method cannot let the object parameter (array) refer to a new object.

Transfer of basic data types: copy the value to the method, but the address values ​​of the two values ​​are different; after the method is called in the main function and pops the stack, the copied value is not used and has no effect on the basic data.

Pass an object or an array as a parameter:  pass the object address value to the method, at this time both the variable and the formal parameter refer to the same object in the heap; and call this object through the method, after the method pops the stack, the parameter variable no longer exists. is used, but the corresponding object variable has changed with the modification of the method.






Guess you like

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