java的值传递和引用传递(能力不行,翻译不准,欢迎纠错)

Does Java pass by reference or pass by value?

Why can't you swap in Java?

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

如果java通过值传递,那不就是一个交换函数的功能吗?

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.

你的问题反应了一个java初学者很容易犯的错误,的确是这样的,甚至经验丰富的老手也会发现连续传值很难。java通过引用操作对象,所有的对象变量都是引用,然而,java不能通过引用传递方法参数,它通过值传递

Take the badSwap() method for example:拿这个badSwap()方法来举个例子吧

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:

当该方法返回时,变量作为参数传递将保持他们的原始值。如果我们将参数类型从int变为object时这个方法会报错,因为java也通过值来传递对象引用,这儿有一个复杂的例子

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:如果我们执行main方法,我们将看到下面的结果

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.

即使该方法是值传递,但它还是成功的输出了pnt1的值。然而,pnt1和pnt2的却没有交换成功。这是最困惑的地方。在main方法中,pnt1和pnt2只不过是对象引用,当你向tricky方法传递pnt1和pnt2时,java像其他的参数一样传递这个引用变量。意思就是说这个引用传递给方法时事实上是把原始引用复制了一份。下面的图形1展示了在java传递一个对象变量给方法时两个引用指向相同的对象

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.

java通过值复制和传递引用变量,因此,方法执行将改变对象,因为那个引用指向了原始的对象。但是,因为那个引用是复制的,交换失败。如图2所示,那个方法交换了引用,但是不是原始的引用。不幸运的是,方法调用过后,你只留下了原始的没交换的引用。对于一个方法调用以外的成功交换,我们需要交换原始的引用,而不是那个复制品。

Figure 2. Only the method references are swapped, not the original ones

 

posted @ 2013-03-19 09:57  歌颂者  阅读(355)  评论(0编辑  收藏  举报