java 板


LINE

※ 引述《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)
1F:推 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)
2F:推 tkcn:謝謝~ 我去研究看看 05/31 14:13
3F:推 godfat:統一名詞沒什麼不好,很多地方by address/reference是一樣的 05/31 14:24
4F:→ csihcs:並不是說統一名詞不好~而是C++的Reference,Java的Reference 05/31 14:27
5F:→ csihcs:是不同的,而C++中by address/reference 又有程度上的差異 05/31 14:29
6F:推 godfat:換另一個說法,其實我找不太到 by address 的說法出處 05/31 14:51
7F:→ godfat:google 只有第一筆符合,還是問問題,下面都無關了 05/31 14:52
8F:推 PsMonkey:樓上的大大,這個拖去 PLT 討論吧 XDXD 05/31 15:07
9F:推 godfat:啊來不及了 XDXD 05/31 15:09
10F:→ csihcs: XDXD 05/31 15:13







like.gif 您可能會有興趣的文章
icon.png[問題/行為] 貓晚上進房間會不會有憋尿問題
icon.pngRe: [閒聊] 選了錯誤的女孩成為魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一張
icon.png[心得] EMS高領長版毛衣.墨小樓MC1002
icon.png[分享] 丹龍隔熱紙GE55+33+22
icon.png[問題] 清洗洗衣機
icon.png[尋物] 窗台下的空間
icon.png[閒聊] 双極の女神1 木魔爵
icon.png[售車] 新竹 1997 march 1297cc 白色 四門
icon.png[討論] 能從照片感受到攝影者心情嗎
icon.png[狂賀] 賀賀賀賀 賀!島村卯月!總選舉NO.1
icon.png[難過] 羨慕白皮膚的女生
icon.png閱讀文章
icon.png[黑特]
icon.png[問題] SBK S1安裝於安全帽位置
icon.png[分享] 舊woo100絕版開箱!!
icon.pngRe: [無言] 關於小包衛生紙
icon.png[開箱] E5-2683V3 RX480Strix 快睿C1 簡單測試
icon.png[心得] 蒼の海賊龍 地獄 執行者16PT
icon.png[售車] 1999年Virage iO 1.8EXi
icon.png[心得] 挑戰33 LV10 獅子座pt solo
icon.png[閒聊] 手把手教你不被桶之新手主購教學
icon.png[分享] Civic Type R 量產版官方照無預警流出
icon.png[售車] Golf 4 2.0 銀色 自排
icon.png[出售] Graco提籃汽座(有底座)2000元誠可議
icon.png[問題] 請問補牙材質掉了還能再補嗎?(台中半年內
icon.png[問題] 44th 單曲 生寫竟然都給重複的啊啊!
icon.png[心得] 華南紅卡/icash 核卡
icon.png[問題] 拔牙矯正這樣正常嗎
icon.png[贈送] 老莫高業 初業 102年版
icon.png[情報] 三大行動支付 本季掀戰火
icon.png[寶寶] 博客來Amos水蠟筆5/1特價五折
icon.pngRe: [心得] 新鮮人一些面試分享
icon.png[心得] 蒼の海賊龍 地獄 麒麟25PT
icon.pngRe: [閒聊] (君の名は。雷慎入) 君名二創漫畫翻譯
icon.pngRe: [閒聊] OGN中場影片:失蹤人口局 (英文字幕)
icon.png[問題] 台灣大哥大4G訊號差
icon.png[出售] [全國]全新千尋侘草LED燈, 水草

請輸入看板名稱,例如:BuyTogether站內搜尋

TOP