Re: Java is "call-by-reference" or "call-by-val …

看板java作者時間20年前 (2004/08/02 14:01), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串1/1
※ 引述《mark00.bbs@bbs.cs.nccu.edu.tw (喵喵小雷貓)》之銘言: : ※ 引述《bubi.bbs@bbs.im.tku.edu.tw (Null)》之銘言: : : 各位大大: : : 如標題所示,沒有人可以幫忙舉例來說明呢? : : 或則哪裡有正確的文章可以參照呢? : 眾多書藉上沒有統一的答案 : 這兩個答案都可以, 看你以什麼角度去解釋 : 以programming language的角度來說, java可以說是call by reference : 因為java不具有pointer的位置運算能力 ......................................................... 就我看的 programming language 的書, java 是 call-by-reference, 除了一些非嚴謹的說法, 無論是 java 正式的 tutorial, 規格書裏, 都是講 java 是 call-by-reference . http://groups.google.com.tw/groups?hl=zh-TW&lr=&ie=UTF-8 &threadm=48K1We%24AWO%40bbs.ee.ntu.edu.tw&rnum=1&prev= /groups%3Fq%3Dpass-by-reference%2520eightcloud %26hl%3Dzh-TW%26lr%3D%26ie%3DUTF-8%26sa%3DN%26tab%3Ddg 寄件者:嵐雲 (EightCloud.bbs@BirdNest.twbbs.org) 主旨:java 沒有 pass-by-reference 那麼另人難接受嗎? View: Complete Thread (共 7 條留言) Original Format 網上論壇:tw.bbs.comp.lang.java 日期:2003-09-17 23:58:19 PST pass-by-xx 是 progamming lanuague 裏, 有關參數傳遞方式的一種分類, 就那分類的定義, java 沒有 pass-by-reference, 而是 pass-by-value, 不管是 java 的技術文件也好, java 的 tutorial 也好, (前面都有給過 URL) 都說 java 沒有 pass-by-reference, 而是 pass-by-value, 這那麼另人難接受嗎? 再從 PL 定義來看 java 的機制, Programming language 對 pass-by-refernece 的定義. " Pass by reference is a second implementation model for inout-mode parameters. Rather than transmittting data values back and forth, however, as in pass-by-value-result, the pass-by-reference method transmits an access path, usually an addesss, to the called subprogram. This provides the access path to the cell storing the actual parameter. Thus the called subprogram is allowd to acesss the actual paramter in the calling program. In effect, the actual parameter is shared with the called subprogram. " java 文件 http://java.sun.com/docs/books/jls/second_edition/html/typesValues.doc.html 4.5.2 Variables of Reference Type A null reference A reference to any object (§4.3) whose class (§4.5.6) is assignmen t compatible (§5.2) with the type of the variable 4.3.1 Objects An object is a class instance or an array. The reference values (often just references) are pointers to these objects, and a special null reference, which refers to no object. java reference type 的 variable(變數), 存的是一個 reference value, 指到 object, 也就是 recerence type variable 的 value 是指到一個 object. 改變一個 refernce type 的 variable 的值, 意義是那個 variable 指到不同的 object, 而不是改變 object 裏的內容. 當做 methoad call 時, 參數(actual parameter) 是 refernce type 的 variable 時, ( "the pass-by-reference method transmits an access path, usually an addesss, to the called subprogram." ) java 在 method call , 有沒有 pass 那個 variable 的 access path, 到被呼叫的 method 裏? 沒有. java 是把 variable 的值, 指到 object 的那個 reference value 傳到 method 裏. 只是 pass-by-value, 讓在 method call 裏, 那個參數變數的 value 等於 actual parameter 的 value, 也就是讓 method call 裏的變數所指向的 object, 等於是 actual parameter 上那個變數所指向的 object 是同一個. 是把變數的值傳進去, 該二個變數指向同一個物件. "This provides the access path to the cell storing the actual parameter" java 在 method 裏, 有沒有辦法去 access 到 actual parameter? 那個 refernce type 的 variable? 不能. java 只是能 access 到 actual parameter 的值, 所指到的 object, 而不是能 access 到 actual parameter 的那個 variable. 也就在 method call 裏, 無法去改變 actual parameter 的值, 也就是, 無法讓 actual parameter 的變數, 指到不同的 object. ( 在 c 裏, 還有個 & 的 operator, 可以取到一個 variable 的 "access path", 所以利用 &, 可以把一個 varialbe的 access path 取出來, 成為模擬 pass-by-reference, 有人稱之為 explicit pass-by-reference. 但是, 那個還是不叫 pass-by-refernce, 因為 actual parameter 是 &variable, 不是 variable. pass 的 是一個 variable 的 unary operator 運算結果的一個 value. 更不用說, 在 java 根本沒有任何方式可取到一個 variable 的 "access path". ) -- ※ Origin: 鳥窩 (BirdNest.twbbs.org) ◆ From: 61-62-68-135-adsl-tpe.STATIC.so-net.net.tw
文章代碼(AID): #113TYX00 (java)
文章代碼(AID): #113TYX00 (java)