Java : 传值or传引用?

那看看这句经典名言:
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.'"

1、对象是按引用操纵的
2、Java 应用程序有且仅有的一种参数传递机制,即按值传递

按值传递意味着当将一个参数传递给一个函数时,函数接收的是原始值的一个副本
按引用传递意味着当将一个参数传递给一个函数时,函数接收的是原始值的内存地址,而不是值的副本


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 below shows two references pointing to the same object after Java passes an object to a method.

 

示例:

 public void tricky(Point arg1, Point arg2)  {
    arg1.x = 100;  
    arg1.y = 100;  
    Point temp = arg1;  
    arg1 = arg2;  
    arg2 = temp;  
} 

示意图:

 

 

 

 

posted @ 2013-09-29 22:07  windlaughing  阅读(196)  评论(0编辑  收藏  举报