Java中方法调用参数传递的方式是传值,尽管传的是引用的值而不是对象的值。(Does Java pass by reference or pass by value?)
原文地址:http://www.javaworld.com/javaworld/javaqa/2000-05/03-qa-0526-pass.html
在Java中,所有的對象變量都是引用,Java通過引用來管理對象。然而在給方法傳參時,Java并沒有使用傳引用的方式,而是采用了傳值的方式。例如下面的badSwap()方法:
public void badSwap(int var1, int var2) {int temp = var1;var1 = var2;var2 = temp; } 當(dāng)badSwap方法時,原有的var1和var2的值并不會發(fā)生變化。即使我們用其它Object類型來替代int,也不會有變化,因?yàn)镴ava在傳遞引用時也是采用傳值的方式。(譯者注:這里是關(guān)鍵,全文的核心是:1. Java中對象變量是引用 2. Java中方法是傳值的 3. 傳方法中參數(shù)時,傳遞的是引用的值)如果譯者的注釋沒看明白,沒關(guān)系,看看下面的代碼:
圖 1. 在作為參數(shù)傳遞后,對象至少有兩個引用指向自己
Java copies and passes the?reference?by value, not the object. Thus, method manipulation will alter the objects, since the references point to the original objects. But since the references are copies, swaps will fail. As Figure 2 illustrates, the method references swap, but not the original references. Unfortunately, after a method call, you are left with only the unswapped original references. For a swap to succeed outside of the method call, we need to swap the original references, not the copies.
在main()中,引用被復(fù)制并以傳值的方式進(jìn)行傳遞,對象本身并不會被傳遞。因此,tricky()方法中pnt1所指向的對象發(fā)生了變化。因?yàn)閭鬟f的是引用的復(fù)制,因此引用的交換既不能引起對象的交換,更不會使原始引用發(fā)生變化。如圖2所示,tricky()交換了arg1與arg2,但不會影響pnt1和pnt2。因此若想交換原始引用pnt1和pnt2,那么不能通過調(diào)用方法的方式來實(shí)現(xiàn)。
圖 2. 只有作為參數(shù)的引用發(fā)生了交換,但原始引用并沒有變化
總結(jié):
1. Java中對象變量是引用?
2. Java中方法是傳值的?
3. 傳方法中參數(shù)時,傳遞的是引用的值
原文如下:
Java does manipulate objects by reference, and all object variables are references. However, Java doesn't pass method arguments by reference; it passes them by value.
Take the badSwap() method for example:
When badSwap() returns, the variables passed as arguments will still hold their original values. The method will also fail if we change the arguments type fromint toObject, since Java passes object references by value as well. Now, here is where it gets tricky:
If we execute this main() method, we see the following output:
X: 0 Y: 0 X: 0 Y: 0 X: 100 Y: 100 X: 0 Y: 0The method successfully alters the value of pnt1, even though it is passed by value; however, a swap ofpnt1 andpnt2 fails! This is the major source of confusion. In themain() method,pnt1 andpnt2 are nothing more than object references. When you passpnt1 andpnt2 to thetricky() method, Java passes the references by value just like any other parameter. This means the references passed to the method are actuallycopies of the original references. Figure 1 below shows two references pointing to the same object after Java passes an object to a method.
Figure 1. After being passed to a method, an object will have at least two references
Java copies and passes the reference by value, not the object. Thus, method manipulation will alter the objects, since the references point to the original objects. But since the references are copies, swaps will fail. As Figure 2 illustrates, the method references swap, but not the original references. Unfortunately, after a method call, you are left with only the unswapped original references. For a swap to succeed outside of the method call, we need to swap the original references, not the copies.
Figure 2. Only the method references are swapped, not the original ones
About the author
Tony Sintes is a principal consultant at BroadVision. Tony, a Sun-certified Java 1.1 programmer and Java 2 developer, has worked with Java since 1997.O'Reilly's Java in a Nutshell by David Flanagan (see Resources) puts it best: "Java manipulates objects 'by reference,' but it passes object references to methods 'by value.'" As a result, you cannot write a standard swap method to swap objects.總結(jié)
以上是生活随笔為你收集整理的Java中方法调用参数传递的方式是传值,尽管传的是引用的值而不是对象的值。(Does Java pass by reference or pass by value?)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: pass by value 与pass
- 下一篇: Onestage Grounding