Java value transfer or reference transfer, memory analysis

Data types are divided into basic data types and reference data types, both of which can be used as method parameters

When the reference type is passed, the address of the data in the heap is passed, so java is passed by value

Next, analyze the memory:
the parameter of the method is the basic data type:
the value of the basic data type is in the stack frame, and a method is a stack frame.
Pass the parameters from the A method to the called B method, and the B method will be in A new stack frame is opened in the stack (into the stack) and a corresponding variable is created. After the method body of the B method is executed, the stack will be popped and the variable will be destroyed, so if there is no return value, after returning to the A method, The value of this parameter will not change.
Insert picture description here
The parameter of the method is the reference data type:
the data of the reference data type is in the heap, and the address in the stack frame is the address of the data in the heap.
The parameters passed between the methods are addresses, so even if there is no return value, you can see Until the data is changed
Insert picture description here

Guess you like

Origin blog.csdn.net/ExceptionCoder/article/details/107261462