Take a variable into a method, and when the method executes, its value sometimes changes and sometimes does not change. At first it would feel-"so magical." When we understand the knowledge of Java memory Analytics, everything is so straightforward-"that's all." But today's experiment class, for reference variables, I made a mistake, the following is the simplification of the code--
void method (int a[]) { int temp[]={1,2,3,4}; A[0]=5; A=temp;}
Here, I try to assign the value of the temporary array variable to a, which changes the value of the argument. If you feel that it is not possible, then there is no need to look down. In fact, of course, it is not possible.
The method passes the parameter, the transfer value and the address, the basic data type variable passes the parameter to pass the value, the reference variable passes the argument is the address. The parameter passed in is actually a copy of the argument. Because, when we invoke a method, the JVM logically creates a memory that we call the stack, and when the method is finished, the memory is freed. The life of the copy of our incoming arguments is limited to this stack area, which is the fate of the local variables. For basic data variables, the function is not affected because its value is present on the stack and is independent of the stack memory of the function that called the method. The value of the reference variable is the address, which points to an address in the heap area, which is a shared memory. Our operation is to manipulate the data in the heap area (continue to see below, this sentence is not rigorous).
This is my understanding of the parameters of the communication. It is no problem to use it to understand today's problems. But it ignores a problem.
Since the incoming reference variable is addressed, why can't the code above change its value?
As we noted above, the argument is actually a copy of the argument, and the reference variable is also present on the stack area. So, my "a= temp" is the data that iscopied from the data that is changed on the stack (their value is only the address, not the object that affects the heap memory). The execution of the method ends, and it is meaningless.
Thus: The method being called, the operation of the heap memory is valid after the execution of the method is completed. For a reference variable, it weighs the object it points to, not itself.
Value is not high, heavy in the record.
Another talk about Java parameters