Neat and clear explanation of Java's passing by value
来源:互联网 发布:常用的python ide 编辑:程序博客网 时间:2024/06/05 13:11
Q: Does Java pass by reference or pass by value?
A: Your question demonstrates a common error made by Java language newcomers. Indeed, even seasoned veterans find it difficult to keep the terms straight.
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:
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 fromint
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: 0X: 0 Y: 0X: 100 Y: 100X: 0 Y: 0
The method successfully alters the value of pnt1
, even though it is passed by value; however, a swap ofpnt1
and pnt2
fails! This is the major source of confusion. In themain()
method, pnt1
and pnt2
are nothing more than object references. When you passpnt1
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 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
Forwarded from: http://www.javaworld.com/javaworld/javaqa/2000-05/03-qa-0526-pass.html
- Neat and clear explanation of Java's passing by value
- Parameter passing in Java - by reference or by value?
- JavaScript: Passing by Value or by Reference
- when love's passing by...
- Explanation of UFT-8 and Unicode
- Explanation of UFT-8 and Unicode
- Passing by reference or by value? it is a question
- Probability of passing exam by guessing
- Java Pass By Value and Pass By Reference
- Simple explanation of Wait Event “cursor: pin s”
- Quick Explanation of Scala’s (_+_) Syntax
- [Excerpt and Translation] Detailed Explanation to Selective Color of Photoshop
- A simple explanation of the Lasso and Least Angle Regression
- hdu 1035 (usage of sentinel, proper utilization of switch and goto to make code neat)
- get static value of class by java reflection
- C++:关于函数调用时的argument passing是by reference还是by value
- Plain Explanation of "memory_order"
- Call-by-reference and Call-by-value
- MySQL日期时间函数大全
- ListCtrl控件
- Ubuntu删除OpenOffice3
- 新浪网站登录过程分析
- Ruby中采用PG访问Postgresql表及function
- Neat and clear explanation of Java's passing by value
- 常见算法:c语言矩阵算法问题
- Xcode4.2 和 iOS 5 的ARC机制及用法介绍
- How to convert Java object to / from JSON (Gson)
- java的同步调用、回调和异步调用。
- opencvAX-day12
- Web前端研发工程师编程能力飞升之路
- Android中对话框的使用
- Extjs GridPanel 显示时间