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中传值还是传引用的认识" Java 中的参数传递是值传递还是引用传递?这是一个经常引发讨论的问题。在 Java 中,参数传递是按值传递的,也就是说,传递给方法的参数是一个副本,而不是原始值本身。 当一个对象...
Python中的参数传递并非简单的"传值"或"传引用",而是一种特殊的机制,通常被称为"传对象引用"。这意味着,当你将一个变量作为参数传递给函数时,实际上是传递了这个变量所引用的对象的引用,而不是对象的副本。 ...
许多人认为对象是按引用传递,而实际上,Java总是按值传递,包括对象。这里的“值”指的是对象的引用,而不是对象本身。这与C++或C#等其他语言中对象的传递方式有所不同。 首先,我们需要理解Java中的引用概念。当...
在Java编程语言中,函数调用时的参数传递方式有两种:传值(Passing by Value)和传引用(Passing by Reference)。虽然Java官方文档中并未明确指出有传引用这一概念,但在实际操作中,Java的行为类似于传引用,尤其...
首先还是应该科普下函数参数传递机制,传值和传引用是什么意思? 函数参数传递机制问题在本质上是调用函数(过程)和被调用函数(过程)在调用发生时进行通信的方法问题。基本的参数传递机制有两种:值传递和引用...
### Java是传值还是传址引用 #### 一、简单类型是按值传递的 Java在处理简单数据类型(如int、boolean等)时采用的是按值传递的方式。这意味着当你将一个简单类型的值作为参数传递给一个方法时,实际上传递的是这...
vue组件在prop里根据type决定传值还是传引用。 简要如下: 传值:String、Number、Boolean 传引用:Array、Object 若想将数组或对象类型也以值形式传递怎么办呢?如下方式可以实现: // component-A 引用component-...
JAVA传值与传引用[整理].pdf
首先还是应该科普下函数参数传递机制,传值和传引用是什么意思? 函数参数传递机制问题在本质上是调用函数(过程)和被调用函数(过程)在调用发生时进行通信的方法问题。基本的参数传递机制有两种:值传递和引用...
当一个函数被调用时,可以通过不同的方式传递参数,其中最常见的是传值(call by value)和传引用(call by reference)两种方法。这两种方式在内存管理、性能影响以及数据修改能力上有着显著的不同。 #### 1. 传值调用...
本篇文章将详细探讨引用类型的传值方式。 首先,理解引用类型的概念至关重要。引用类型包括类(classes)、接口(interfaces)、数组以及委托(delegates),它们在内存中的存储方式与值类型截然不同。对于引用类型...
在PHP编程中,理解变量的传值和传引用是非常重要的概念,它们决定了函数内部操作对原始变量的影响。下面我们将详细探讨这两个概念的区别、工作原理以及优缺点。 **1. PHP传值** 当一个变量作为参数传递给函数时,...
这在一定程度上类似于引用传递,但程序员无法直接控制参数是传值还是传引用,LabVIEW会根据安全性优先、效率其次的原则自动决定。 为提高程序效率,LabVIEW程序员可以通过设计程序结构来引导LabVIEW识别哪些部分...
"Java中的传值与传引用实现过程解析" Java中的传值与传引用是Java编程语言中的一种基础概念,它们是Java函数中参数传递的两种方式。 Java中的传值是指函数参数的值被复制到函数内部,在函数内部对参数的修改不会...
在编程世界中,传值和传引用是两个基础但至关重要的概念,它们直接影响着程序的运行方式和数据处理。在LabVIEW(Laboratory Virtual Instrument Engineering Workbench)这种图形化编程语言中,理解这两个概念对于...
在计算机编程领域,特别是对于面向对象语言如Java而言,“传值”与“传引用”的概念是理解数据传递机制的关键所在。Java作为一种广泛使用的高级编程语言,在设计之初就考虑到了如何避免C/C++等语言中的复杂性,特别...