关于java的传值和传引用

转载 2013年12月02日 17:21:35

QIf Java uses the pass-by reference, why won't a swap function work?

AYour 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 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

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. 

相关文章推荐

Java是传值还是传址引用

  • 2007年04月20日 17:28
  • 53KB
  • 下载

一个实例介绍——java中的参数传递(传值、传引用)

java的学习之路总是bup

Java到底是传引用还是传值?

转自http://www.importnew.com/3559.html Java确实使用对象的引用来做计算的,所有的对象变量都是引用。但是,Java在向方法传递参数时传的不是引用,是值。 以...

JAVA中的引用到底是传值还是传址?

JAVA中的引用到底是传值还是传址? JAVA中除了8种基本类型外,其它的类型是引用类型,像STRING,数组,文件流等。引用变量在JAVA中是一个存储对象在内存中的地址的变...

Java参数传值还是传引用

Java参数传值还是传引用  参数是按值而不是按引用传递的说明 Java 应用程序有且仅有的一种参数传递机制,即按值传递。写它是为了揭穿普遍存在的一种神话,即认为 Java 应用程...
  • xys_777
  • xys_777
  • 2011年11月09日 10:43
  • 18020

关于Java对象作为参数传递是传值还是传引用的问题

前言  在Java中,当对象作为参数传递时,究竟传递的是对象的值,还是对象的引用,这是一个饱受争议的话题。若传的是值,那么函数接收的只是实参的一个副本,函数对形参的操作并不会对实参产生影响;若传的是引...

Java到底是传引用还是传值?

问题: 如果Java是用引用来传递的话,为什么交换函数(swap)不起作用呢? 回答: 你的问题引出了Java新手的常犯的错误。事实上,一些老手也很难搞清楚这些概念。 Java确实使用对象的引用来...

java传值与传引用的三种情况

基本数据类型传递的是值的拷贝;对象类型传递的是引用的拷贝;而String类型传递的虽然也是对象,但它不同于一般的对象类型,它String被设计为不可修改的类型,也就是对String对象的任何修改都将重...

Java传值传引用之浅显解析

Java的基本类型和引用类型的传值还是传引用
  • tseki
  • tseki
  • 2015年05月28日 00:47
  • 236

Java形参是传值还是传引用

Java 方法的参数是简单类型的时候,是按值传递的 (pass by value)。这一点我们可以通过一个简单的例子来说明: /* 例 1 */ /**  * @(#) Test.java ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:关于java的传值和传引用
举报原因:
原因补充:

(最多只允许输入30个字)