Problem of method parameter passing in Java
It can be understood that when we call a method, we pass the specified value to the parameter in the method, so that the parameter in the method has the specified value, which can be used to operate in the method. This way of passing, we call it parameter passing. Here, when defining a method, a variable in the argument list, which we call the formal parameter.
When calling a method, the numeric value passed to the method, which we call the actual parameter
When you call a method in Java, the change in the formal parameter does not affect the actual parameter if the parameter is a primitive type (Byte/short/int/long/float/double/char/boolean) and a string type.
The following code takes action in memory:
Initially, the method area is stored in the main () method file, and then in the stack the main () method into the stack (stack), the local variable into the stack initialization, execute to the change () method, the method area has a change () file, and then change () into the stack, the argument A/ b assigns its own value to the parameter A/b, executes the code in the method, parameter a becomes 20, parameter B becomes 40, and after execution, the change () method immediately pops out of the stack (the stack), and the parameter A/b is also removed from memory. The period does not affect the value of the argument.
If the reference data type (not including string), such as Array int[], the parameter changes affect the argument.
Initially, the method area is stored in the main () method file, and then in the stack main () method into the stack (stack), create a static method to create an array, equivalent to a new int[], all new out to go into the heap, in the heap to draw a memory space, divided into 5 parts to store 5 elements, The value of the first initialization element is all 0, then each variable is assigned a value of 1,2,3,4,5, and then the open memory address 0x1234 is assigned to arr. Execute to the change () method, have the change () file in the method area, then change () into the stack, the argument arr assigns the address value to the parameter arr, the arguments and the parameters point to the memory that is opened up in the heap, and then in the change (), the even-numbered elements in the array are changed to twice times, The change is the data in the heap memory. Then the change () method executes out of the stack, the main method prints the array, and the argument arr still points to the changed array in memory and outputs. The parameter changes affect the value of the argument.
For a particular string type, it is a reference data type, but it is processed here by basic data. Because the nature of the string is a character array, there is a special piece of space in the method area called a string constant pool used to store string constants. Here, remember that all double quotation marks are string constants stored in the string constant pool. The "Hello" string is stored in the string constant pool with the address value 0x666 , then stirng str1 = new String ("Hello"), creating a piece of memory in the heap, storing the address value of "Hello" 0x666 in memory, and assigning the address value of that memory 0x999 to str1. Then String str2 = "Hello", Assign the address value of "Hello" 0x666 directly to str2.
String has a feature, each change will be in the string constant pool to generate a new constant, such as string s = "Hello" in the string constant pool address is 0x666, and then s = "Hello" +1, at this time the string constant pool will open a new space to " Hello1 ", the address value is 0x333, and the address value is assigned to S. So, String s =" Hello ", when you pass s as an argument to a method, no matter how the s change in the formal parameter does not affect the 0x666" Hello ", It does not affect the argument s. So string, although as a reference data type, does not affect the argument's parameter changes.
Problem of method parameter passing in Java