Java method parameters can only be passed by value!

In common parlance, the transfer of method parameters is divided into two types: value transfer and reference transfer. Value transfer refers to the transfer of a copy of the actual parameters to the method,

Changes in the method will not affect the actual parameter itself, while reference passing means that the actual parameter itself is passed, and changes in the method will affect the actual parameter itself

The parameter itself. However, in Java, there is only value passing, not reference passing! So why is value passing when the method parameter is a basic data type,

When it is a reference type, what is the form of reference passing?

The JAVA memory area contains two memory areas: the Java heap and the virtual machine stack (not only the JAVA memory is divided into these two memory areas, but also the process

Sequence counter, local method stack and method area), the purpose of Java heap is to store objects. When a method is executed, each method will create its own

The memory stack is used to store information such as variables defined in the method. When the method ends, the memory stack of the method will also be destroyed.

In general, the Java method runtime is stored in the stack and the object itself is stored in the heap. The following two examples will describe when the method parameter is a basic data type

Java's value passing process when and referencing types.

When the method parameter is a basic data type:

It can be seen that the a passed by the swap method in the main method, the b parameter is only a, B copy, not a, B itself, JAVA memory space below.

The value passing of Java method parameters is further explained.

When the main method starts to execute, the main method stack is created. Two variables A and B are stored in the stack, with values of 9 and 20 respectively. Main calls the swap method and passes a,

The copy of B is given to the swap method. The swap method creates its own stack area with a, B and temp. At this time, there are five temporary variables in the memory area. In swap, a, B

After the swap method ends, the swap stack area is destroyed. From beginning to end, a and B in the main stack area are not affected until the main method ends and the main stack is destroyed

Area destroyed. So Java passes only a copy of the parameter, not the parameter itself.

When the parameter is a reference type:

From the above example, when the parameter type is reference, the original parameter is affected. Is reference passing? The answer is no, parameter

Or value transfer.

There is a testvaluetransfer reference variable in the main method, which is saved in the main method stack, and the new testvaluetransfer object is saved in the Java heap

, testvaluetransfer actually stores the address of the object and points to the object. When calling the swap method in the main method, the testValueTransfer reference change is passed.

At this time, there are two variables in the memory area pointing to the testvaluetransfer object. When the A and B member variables in the object are exchanged in the swap method, the actual operation is

It is the object itself, so when the swap method ends, the object pointed to by testvaluetransfer in the main method changes.

Summary: the Java parameter transfer method is only value transfer, and the passed parameters are always copies of the original parameters.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>