Does Java pass by reference or pass by value?
来源:互联网 发布:通州淘宝城在哪个位置 编辑:程序博客网 时间:2024/05/16 11:58
Does Java pass by reference or pass by value?
Q: If Java uses the pass-by reference, why won't a swap function work?
A: 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 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: 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 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 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 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.
Java copies and passes thereference 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 onesO'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.
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.Learn more about this topic
- Practical Java Programming Language GuidePeter Haggar (Addison-Wesley 2000)
http://www1.fatbrain.com/asp/bookinfo/bookinfo.asp?theisbn=0201616467 - Java in a Nutshell David Flanagan (O'Reilly and Associates 1999)
http://www1.fatbrain.com/asp/bookinfo/bookinfo.asp?theisbn=1565924878
- [转]Does Java pass by reference or pass by value?
- Does Java pass by reference or pass by value?
- Does Java pass by reference or pass by value?
- Does Java pass by reference or pass by value?
- Is Java pass by VALUE or pass by REFERENCE?
- Pass By Value or Pass By Reference in java
- 002:Is Java “pass-by-reference” or “pass-by-value”?
- Java: pass by value or reference?
- Pass by value & pass by reference
- pass by value & pass by reference
- pass by value vs pass by reference
- java pass by value 。do not exist pass by reference
- Java is Pass-by-Value, Never pass by reference
- Java Pass By Value and Pass By Reference
- pass by value 与pass by reference 小结
- 关于pass-by-reference-to-const和pass-by-value
- Pass by reference和pass by value区别举例
- 关于 pass-by-reference-to-const 和 pass-by-value
- iOS中UIWindow的获得
- Java 加密解密之对称加密算法DES
- java中常见的查找方式
- Android点击除EditText外地方隐藏软键
- Android中adapter调用notifyDataSetChanged无效原因总结
- Does Java pass by reference or pass by value?
- Java 加密解密之对称加密算法DESede
- Levmar 配置
- UGUI实现操纵摇杆
- CodeForces 670B、HDU 1003、CodeForces 632C
- Trouble Shoot - The prefix "context" for element "context:component-scan" is not bound. Spring MVC
- HDU 2896 病毒侵袭
- LinearLayout及其常用属性
- javascript框架思想1