看板 java 關於我們 聯絡資訊
※ 引述《tkcn (小安)》之銘言: : ※ 引述《Holocaust123 (Terry)》之銘言: : : 由此可知 java 的函數若以陣列為引數,預設是傳址呼叫。 : : 有沒有辦法改成傳值呼叫呢? : 1. Java 沒有 Poninter,只有 Reference, : 所以根本也不會有 Call by Address。 : 2. Java 也沒有 Call by Reference, : 全部都只是 Call by Value。 : 你的程式遇到的情形, : 其實是對 reference 變數做了 Call by Value, : 因此傳入的參數是另一份 reference 但指向同一個物件, : 對 reference 變數來說,這是 Call by Value。 : 詳情請參考本版 5054 討論串。 : (光是本版大概就討論了三次以上囉,不過比較完整的我只找到 5054 這串) : 要解決你的問題, : 請自己對陣列進行複製。 就我的認知上 C 的 call by address 是 call by value of pointer 而Java的call by reference與C++的call by reference 是不一樣的 Java的call by reference與C的call by value of pointer比較像 理由如下: C++中 call by value of pointer call by reference of pointer 指標(Pointer) (type *p) || 參照(Reference) (type &*p) 記憶體配置 配置指標記憶體(4Byte) || 不配置記憶體,本身為別名(Alias) 你可以改變所指的物件狀態 你可以改變所指的物件狀態 但是不能改變所指的物件 也可以改變所指的物件 class test { public: test(int in) { i = in; } int i; }; void func11(test *input) { void func21(test *&input) { *input.i = 11; *input.i = 21; } } void func12(test *input) { void func22(test *&input) { input = new test(12); input = new test(22); } } int main(int argc, char *argv[]) { test *main_input = new test(100); cout << *main_input.i << endl; // 100 func11(main_input); cout << *main_input.i << endl; // 11 func21(main_input); cout << *main_input.i << endl; // 21 func12(main_input); cout << *main_input.i << endl; // 21 而非 12 func22(main_input); cout << *main_input.i << endl; // 22 return 0; } 而 java 中,method 呼叫狀況與前面call by value of pointer是相同的 class JavaTest { int i; JavaTest(int in) { i = in; } public static void method1(JavaTest input) { input.i = 1; } public static void method2(JavaTest input) { input = new JavaTest(2); } public static void main(String[] argv) { JavaTest mainInput = new JavaTest(100); System.out.println(JavaTest.i); // 100 method1(mainInput); System.out.println(JavaTest.i); // 1 method2(mainInput); System.out.println(JavaTest.i); // 1 而非 2 } } -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 211.74.9.2 陣列的部分,你可以改變陣列中的資料,但不能改變呼叫者變數儲存不同陣列, 其中陣列中的資料可視為陣列的狀態。 class Test { public static void func1(int [] arr) { arr[0] = 10; } public static void func2(int [] arr) { arr = new int[2]; } public static void main(String [] argv) { int [] mainArr = new int[5]; mainArr[0] = 100; System.out.println(mainArr[0]); // 100 func1(mainArr); System.out.println(mainArr[0]); // 10 System.out.println(mainArr.length); // 5 func2(mainArr); System.out.println(mainArr.length); // 5 } } ※ 編輯: csihcs 來自: 211.74.9.2 (05/31 12:51)
tkcn:Java 沒有 Call by Reference 05/31 13:19
java 有這個詞但是與C++的是不同的 在昇揚 java 網站線上教學裡面提到 http://java.sun.com/developer/onlineTraining/JavaIntro/contents.html#ReferenceVariableUsage http://tinyurl.com/n6mqfo [上面網址的縮址/不放心可點上面的] When the argument and parameter types are nonprimitive (a defined class), this process is generally called call by reference because the invoked method receives a copy of a reference value. ※ 編輯: csihcs 來自: 211.74.9.2 (05/31 13:52)
tkcn:謝謝~ 我去研究看看 05/31 14:13
godfat:統一名詞沒什麼不好,很多地方by address/reference是一樣的 05/31 14:24
csihcs:並不是說統一名詞不好~而是C++的Reference,Java的Reference 05/31 14:27
csihcs:是不同的,而C++中by address/reference 又有程度上的差異 05/31 14:29
godfat:換另一個說法,其實我找不太到 by address 的說法出處 05/31 14:51
godfat:google 只有第一筆符合,還是問問題,下面都無關了 05/31 14:52
PsMonkey:樓上的大大,這個拖去 PLT 討論吧 XDXD 05/31 15:07
godfat:啊來不及了 XDXD 05/31 15:09
csihcs: XDXD 05/31 15:13