Does Java pass by reference or pass by value?
Why can't you swap in Java?
By Tony Sintes, JavaWorld.com, 05/26/00
Q:If Java uses the pass-by reference, why won't a swap function work?
A: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;
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);
- }
-
-
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
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 中,参数传递是按值传递的,也就是说,传递给方法的参数是一个副本,而不是原始值本身。 当一个对象...
在Java编程语言中,函数调用时的参数传递方式有两种:传值(Passing by Value)和传引用(Passing by Reference)。虽然Java官方文档中并未明确指出有传引用这一概念,但在实际操作中,Java的行为类似于传引用,尤其...
理解Java中的传值与传引用对于编写正确且预期的行为代码至关重要。在编写函数时,应清楚地知道参数如何被传递,以及这将如何影响函数的可读性和行为。在实际编程中,合理运用这些知识能帮助避免许多常见的错误和困惑...
本文将深入探讨Java中的方法调用、值传递与引用传递,并通过字节码分析来进一步理解这些概念。 首先,我们来看方法调用。在Java中,方法是一组完成特定任务的代码块,可以通过方法名来调用执行。方法调用的基本语法...
在计算机编程领域,特别是对于面向对象语言如Java而言,“传值”与“传引用”的概念是理解数据传递机制的关键所在。Java作为一种广泛使用的高级编程语言,在设计之初就考虑到了如何避免C/C++等语言中的复杂性,特别...
引用类型的传值行为体现在函数调用和方法参数传递上。当我们把一个引用类型变量作为参数传递给函数时,并不会创建该对象的新副本,而是复制了这个引用。这意味着函数内部对对象的任何修改都会影响到原始对象,因为...
值传递和引用传递的概念对于理解Java中的方法调用至关重要。基本类型参数的值在方法内部的任何修改都不会影响到方法外部的原始变量。而引用类型参数虽然看起来似乎可以修改外部变量,但实际上是通过修改对象的状态来...
因此,当调用`append`方法时,它是在原始对象上进行操作,而不是创建一个新的对象。这就是为什么`main`方法中的`sb`对象值被改变了,因为`change`函数中的`sb`和`main`方法中的`sb`都指向同一个内存地址。 3. **...
例如,从Java调用到C++: ```java public native int addNumbers(int a, int b); ``` 对应的C++实现: ```cpp JNIEXPORT jint JNICALL Java_com_example_MyClass_addNumbers(JNIEnv *env, jobject /* this */,...
### Java及C++中传值传递、引用传递和指针方式的理解 在程序设计语言中,函数调用时参数的传递方式对理解程序的行为至关重要。本文将深入探讨Java与C++这两种广泛使用的编程语言中参数传递的方式,包括值传递、引用...
Java方法参数传值是Java编程中的基础知识点,涉及变量、数据类型和方法调用的机制。在Java中,数据类型分为基本数据类型和引用数据类型。基本数据类型包括布尔型(boolean)、字节型(byte)、短整型(short)、整型...
大多数程序设计语言具有传值调用和传引用调用的两种方法.通过对典型程序的研究与分析可以看出JAVA语言的参数传递总是传值调用的,但是对于基本类型的参数和对象类型的参数来说,参数传递的情况不完全相同.Java语言不能...
在Java编程语言中,参数传递是一个关键的概念,它涉及到函数或方法调用时,值如何从调用者传递到被调用者的过程。Java只支持两种类型的参数传递:基本数据类型(如int、double等)的传值和引用类型(如类对象)的...
当我们讨论“引用类型传值”时,意味着在方法调用时,传递的是对象引用的一个副本,而不是对象本身。以下是对给定代码清单的详细解释: 1. `Circle.java` 文件定义了一个名为 `Circle` 的类,它包含一个 `double` ...
在调用`sendObject`方法时,传入的是`user`对象的引用。尽管在方法内部创建了一个新的`User`对象,并将其名称设置为“李四”,但这个新对象并没有被赋值给`user1`,因此`user1`仍然指向原始的`User`对象。由于没有对...
传名在Python中可以被视为一种特殊形式的传引用,因为Python中的所有对象都是引用类型,函数调用时传递的是对象的引用。这意味着,即使在函数内部修改形参,也会直接影响到实参。 最后,我们来探讨**传地址**。传...
在编程语言中,参数传递是函数调用时传递实参给形参的方式。Java和C++对待参数传递有着不同的处理机制,这主要体现在值传递、引用传递和指针方式上。 首先,我们来看Java的值传递。Java中,所有的参数传递都是基于...
### Java 类:传值还是传引用? 在Java编程语言中,理解变量的传递方式对于编写高效、可维护的代码至关重要。本文将通过一个具体的例子来深入探讨Java中的传值与传引用的区别,并解释如何利用这一特性来更好地管理...
这在一定程度上类似于引用传递,但程序员无法直接控制参数是传值还是传引用,LabVIEW会根据安全性优先、效率其次的原则自动决定。 为提高程序效率,LabVIEW程序员可以通过设计程序结构来引导LabVIEW识别哪些部分...