java method parameter passing only ---- values passed!

  In conventional parlance, the parameter delivery method is divided into two, and transmitted values ​​passed by reference, refers to the value passed to a parameter passed to the actual copying process,

Changes in the process will not affect the actual parameter itself, the transmission means is passed by reference is the actual parameters themselves, the changes in the process will affect the real

Inter parameters themselves. However, only the value passed in java, is not passed by reference! Why, then, when the method is when the performance parameters of basic data types are passed by value,

And when is the performance of the reference type is passed by reference in the form it?

  Java heap memory area containing the java virtual machine stack and two memory areas (not just the java memory area is divided into two memory areas, in addition to Cheng

The program counter, native method stacks and method area), java heap purpose is to store the object, when a method of execution, each method will build for yourself

Memory stack for variables defined in the information storage method, when the end of the method, which the memory stack will be destroyed.

  In general, java method of storing the runtime stack and the object itself is present in the stack. Two examples will now be described when the method parameter is the basic data type

When the reference value and when the type of transfer process java.

  The method of basic parameter data type:

 

 

 

   As can be seen, the main method to call swap method is passed a, b, only the parameter a, b copy, not is not a, b itself, memory space will java

Case, the value passed further described java method parameters.

  When the main method begins execution, the main method to create a stack, the stack is stored a, b two variables, values ​​of 9,20, respectively. main calls swap method and pass a,

B copy method to swap, swap method to create your own stack area, there are a, b and temp, 5 temporary variable exists in the memory area at this time, in the swap a, b

Exchange, when the end of the swap method, swap stack area is destroyed, from beginning to end, in the main stack area a, b are not affected, until the end of the main method, main stack

Area destroyed. So rather than simply copy parameter parameter itself java passed.

 

  And when the argument is a reference type:

 

 

 

   From the above example, when the parameter type is a reference, the original parameters have been affected, whether it is made pass a reference to it? The answer is no, parameters

Or perform value transfer.

   在main方法中有testValueTransfer引用变量,它保存在main方法栈中,新new的TestValueTransfer对象则保存在java堆中

,testValueTransfer实际存储的是对象的地址指向对象。当在main方法中调用swap方法时,传递的是testValueTransfer引用变

量的副本,此时内存区中存在两个变量指向TestValueTransfer对象,当在swap方法中交换对象中的a,b成员变量时,实际操作

的是对象本身,因此当swap方法结束后,main方法中testValueTransfer指向的对象发生了变化。

 

   总结:java参数传递方式只有值传递一种方式而已,传递的参数一直都是原始参数的副本。

Guess you like

Origin www.cnblogs.com/lin0/p/12356607.html