java传值还是传引用

最近一直在看primer c++,函数一章对c++函数之间传递参数有详细的讲解,传值、传指针和传引用的应用与区别都讲得很清楚,
以前一直用java,由于java没有指针,当时也没有引用这个概念,所以没有对java参数的传递有过多的思考,现在搞清楚了c++
传递参数的机制,自然就会跟java进行对比,究竟java传的是什么东西?
Java代码
public static void test(Pass passA); {   
passA = null;  
}
看看, 对passA有什么影响?
毫无作用。函数调用出来后,passA还是原来的值,不会变成Null.

但是,你的代码对 passA进行了操作  passA.a ,改变了passA的成员变量。
这个成员变量是一个真实指向String 的 地址,当然能够被改变。
这就是操作 (.)  和 赋值 (=) 的区别。
这是对 成员变量 a 的 赋值。真正改变了成员变量 a 的值。

注意,这里传递的参数是 passA, 而不是 a.
所以,passA 被复制了一份。passA 的这个副本的 a 变量还 指向 原来的 passA 的 a 变量。
Java中的“引用”是指非原生类型的变量的值保存的是对一个堆上对象的引用。
Java任何时候都是传值的,只不过该值的语义有所不同罢了

1、对象是按引用传递的
2、Java 应用程序有且仅有的一种参数传递机制,即按值传递
3、按值传递意味着当将一个参数传递给一个函数时,函数接收的是原始值的一个副本
4、按引用传递意味着当将一个参数传递给一个函数时,函数接收的是原始值的内存地址,而不是值的副本
写的没错,但是文字太多,第二条就已经把人弄糊涂了,得仔细看完4条才清楚。而且对String类型的疑惑没有解决。

三句话总结一下:

1.对象就是传引用

2.原始类型就是传值

3.String类型因为没有提供自身修改的函数,每次操作都是新生成一个String对象,所以要特殊对待。可以认为是传值。
在网上查了一下,还是外国人解释得比较清楚:
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代码
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:

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);    
}  
 
                    

 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代码
 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值