`
ethen
  • 浏览: 119745 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Java 方法调用传值还是传引用

阅读更多

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:

Java代码 复制代码
  1. public void badSwap(int var1, int var2)   
  2.   
  3.  int temp = var1;   
  4.  var1 = var2;   
  5.  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:

Java代码 复制代码
  1.  public void tricky(Point arg1, Point arg2)   
  2. {   
  3.   arg1.x = 100;   
  4.   arg1.y = 100;   
  5.   Point temp = arg1;   
  6.   arg1 = arg2;   
  7.   arg2 = temp;   
  8. }   
  9. public static void main(String [] args)   
  10. {   
  11.   Point pnt1 = new Point(0,0);   
  12.   Point pnt2 = new Point(0,0);   
  13.   System.out.println("X: " + pnt1.x + " Y: " +pnt1.y);    
  14.   System.out.println("X: " + pnt2.x + " Y: " +pnt2.y);   
  15.   System.out.println(" ");   
  16.   tricky(pnt1,pnt2);   
  17.   System.out.println("X: " + pnt1.x + " Y:" + pnt1.y);    
  18.   System.out.println("X: " + pnt2.x + " Y: " +pnt2.y);     
  19. }   
  20.   
  21.                      
 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:

Java代码 复制代码
  1.  X: 0 Y: 0  
  2. X: 0 Y: 0  
  3. X: 100 Y: 100  
  4. 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 

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 之方法调用 方法传参 值传递还是引用传递字节码

    NULL 博文链接:https://m635674608.iteye.com/blog/1513061

    Java语言的方法参数浅析

    大多数程序设计语言具有传值调用和传引用调用的两种方法.通过对典型程序的研究与分析可以看出JAVA语言的参数传递总是传值调用的,但是对于基本类型的参数和对象类型的参数来说,参数传递的情况不完全相同.Java语言不能...

    java及C++中传值传递、引用传递和指针方式的理解

    为什么 Java 只有值传递,但 C++ 既有值传递,又有引用传递呢?今天我们就来探讨下这个问题,有需要的朋友可以参考下

    Java开发技术大全(500个源代码).

    invokeMethod.java 同一个类中调用方法示例 invokeOther.java 类的外部调用方法示例 invokeStaticMethod.java 调用静态方法示例 localVariable.java 演示局部变量 localVSmember.java 局部变量与成员变量同名...

    Java中对象的销毁

     如从被调用函数参数引用传值或返回值到主调用函数所在的对象类型变量中,则该对象都仍存在(但被调用函数的该对象的引用变量生命周期结束,因此引用变量是局部变量),此时对象突破了局部变量的局部生命期。...

    c#学习笔记——学习心得

    可以被这个类或者结构的所有成员函数(方法、构造函数)使用,可以是值类型或引用类型,主要有实例字段和静态字段。区别于局部变量在于局部变量只能被给定的函数或代码块使用 属性:一种用于访问对象或类的特性的成员...

    Android知识点及重要代码合集 word文档

    14.4 数据库的使用(调用已封装SQL语句的方法) 200 14.5 简单游标适配器的使用及分页效果的实现 207 15.1对手机通讯录的增删改查 211 15.2查询手机通话记录 221 15.3操作手机短信 224 16.1 自定义内容提供者的编写...

    C语言解析教程(原书第4版)(美) 凯利.pdf

    6.3 传引用调用 6.4 数组和指针之间的关系 6.5 指针运算和元素的大小 6.6 数组作为函数的实参 6.7 例子:冒泡排序 6.8 用calloc()和malloc()进行动态内存分配 6.9 例子:归并和归并排序 6.10 字符串 6.11 标准...

    asp.net知识库

    动态调用对象的属性和方法——性能和灵活性兼备的方法 消除由try/catch语句带来的warning 微软的应试题完整版(附答案) 一个时间转换的问题,顺便谈谈搜索技巧 .net中的正则表达式使用高级技巧 (一) C#静态成员和...

Global site tag (gtag.js) - Google Analytics