Does Java pass by reference or pass by value?
Why can't you swap in Java?
By Tony Sintes, JavaWorld.com, 05/26/00
If Java uses the pass-by reference, why won't a swap function work?
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 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.
分享到:
相关推荐
java中传值还是传引用的的认识? 当一个对象被当作参数传递到一个方法后,此方法可改变这个对象的属性,并可返回变化后的结果,那么这里到底是值传递还是引用传递?
python基础入门教程:传参是传值还是传引用
传值还是传引用的问题,到此已经算是解决了,但是我们仍然不能解决这样一个问题:如果我有两个 int 型的变量 a 和 b,我想写一个方法来交换它们的值,应该怎么办? 结论很让人失望——没有办法!因此,我们只能...
前言在Java中,当对象作为参数传递时,究竟传递的是对象的值,还是对象的引用,这是一个饱受争议的话题。若传的是值,那么函数接收的只是实参本,函数对形参的操作并不
java中传值与传引用,详细讲解java中的传值与传引用
Java是传值还是传址引用
本篇文章主要介绍了详解python函数传参是传值还是传引用,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
主要介绍了python 函数传参之传值还是传引用的分析,需要的朋友可以参考下
vue组件在prop里根据type决定传值还是传引用。 简要如下: 传值:String、Number、Boolean 传引用:Array、Object 若想将数组或对象类型也以值形式传递怎么办呢?如下方式可以实现: // component-A 引用component-...
JAVA传值与传引用[整理].pdf
引用类型不像值类型传值那样直接,那么引用类型是怎么样传值的呢?答案就在此文档中。
在本篇文章里小编给大家整理的是关于php传值和传引用的区别点总结,需要的朋友们可以参考下。
android activityIntent 传值 传对象
主要介绍了java中的传值与传引用实现过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
传值和传引用的问题一直是Java里争论的话题。与C++不同的,Java里面没有指针的概念,Java的设 计者巧妙的对指针的操作进行了管理。事实上,在懂C++的Java程序员眼中,Java到处都是精美绝伦的指 针。
主要介绍了Python参数传递机制传值和传引用原理详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
C语言函数调用三种方式 传值调用,引用调用和传地址调