那看看這句經典名言:
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.'"shell
一、對象是按引用操縱的
二、Java 應用程序有且僅有的一種參數傳遞機制,即按值傳遞函數
按值傳遞意味着當將一個參數傳遞給一個函數時,函數接收的是原始值的一個副本
按引用傳遞意味着當將一個參數傳遞給一個函數時,函數接收的是原始值的內存地址,而不是值的副本spa
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.code
示例:對象
public void tricky(Point arg1, Point arg2) { arg1.x = 100; arg1.y = 100; Point temp = arg1; arg1 = arg2; arg2 = temp; }
示意圖:blog