精华帖 (0) :: 良好帖 (0) :: 新手帖 (2) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2008-06-12
by http://www.javaworld.com/javaworld/javaqa/2000-05/03-qa-0526-pass.html public void badSwap(int var1, int var2) { int temp = var1; var1 = var2; var2 = temp; }
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 from int to Object, since Java passes object references by value as well. Now, here is where it gets tricky: public void tricky(Point arg1, Point arg2) { arg1.x = 100; arg1.y = 100; Point temp = arg1; arg1 = arg2; arg2 = temp; } public static void main(String [] args) { Point pnt1 = new Point(0,0); Point pnt2 = new Point(0,0); System.out.println("X: " + pnt1.x + " Y: " +pnt1.y); System.out.println("X: " + pnt2.x + " Y: " +pnt2.y); System.out.println(" "); tricky(pnt1,pnt2); System.out.println("X: " + pnt1.x + " Y:" + pnt1.y); System.out.println("X: " + pnt2.x + " Y: " +pnt2.y); } 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: 0 The method successfully alters the value of pnt1, even though it is passed by value; however, a swap of pnt1 and pnt2 fails! This is the major source of confusion. In the main() method, pnt1 and pnt2 are nothing more than object references. When you pass pnt1 and pnt2 to the tricky() method, Java passes the references by value just like any other parameter. This means the references passed to the method are actually copies 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 Author Bio 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.
声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2008-06-12
写的不错,有机会我在翻译下。
|
|
返回顶楼 | |
发表时间:2008-06-12
另参考:http://www.yoda.arachsys.com/java/passing.html
|
|
返回顶楼 | |
发表时间:2008-08-06
虽然很白,但还是要问,哪位高手能告诉我
original references
object references
object
上面三个短语是什么意思啊?
|
|
返回顶楼 | |
发表时间:2008-08-06
lihongjunzw 写道
虽然很白,但还是要问,哪位高手能告诉我 original references object references object 上面三个短语是什么意思啊?
object references 对象引用 object 对象 |
|
返回顶楼 | |
发表时间:2008-10-16
请注意,String是很特殊的,参看文章http://www-128.ibm.com/developerworks/cn/java/l-jpointer/index.html
|
|
返回顶楼 | |
浏览 3376 次