




下載本文檔
版權(quán)說(shuō)明:本文檔由用戶(hù)提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、java參數(shù)傳遞時(shí)到底是值傳遞還是弓I用傳遞(baidu搜集)最近比較長(zhǎng)一段時(shí)間以來(lái),網(wǎng)上的IT同行里面比較流行“JAVA面試32問(wèn)”,很 多人的BLOG里都引用這些面試題,最近因?yàn)楣ぷ鲀?nèi)容比較枯燥,也來(lái)看看這 些試題以調(diào)節(jié)一下口味,其中有一道題讓我很費(fèi)解。原題是:當(dāng)一個(gè)對(duì)象被當(dāng)作參數(shù)傳遞到一個(gè)方法后,此方法可改變這個(gè)對(duì)象的屬性,并可返回變化后的結(jié)果,那么這里到底是值傳遞還是引用傳遞?用google查詢(xún)結(jié)果,得到答案基本上是:值傳遞。當(dāng)時(shí)覺(jué)得挺納悶兒,為什么 連參數(shù)的內(nèi)容都被修改了,怎么還能說(shuō)是“值傳遞”呢?因?yàn)樵趥鹘y(tǒng)的印象里(尤其是從C+過(guò)來(lái)以后),值傳遞都應(yīng)該是不改變?cè)瓍?shù)的。問(wèn)問(wèn)周?chē)耐?/p>
2、事,也大都這么講,但是也都講不清這種理論的根源是什么。我這 個(gè)人有個(gè)毛病,有事情想不通時(shí)就會(huì)憋得難受,后來(lái)在Thinking in Java的 一段內(nèi)容(注解1)里找到了自己的結(jié)論,我認(rèn)為(Thinking in Java的作 者也這么認(rèn)為):可以說(shuō)是值傳遞,也可以說(shuō)是引用傳遞。一,認(rèn)為是值傳遞。得出這種結(jié)論的前提必須是“參數(shù)的值就是對(duì)該對(duì)象的引用, 而不是對(duì)象的內(nèi)容”,這句話可能有些費(fèi)解,舉個(gè)例子加以說(shuō)明。public class Paier public static void main(S tringU args) Paier paier = new PaierO; paier. tes
3、tO;public void testO Test Class paral = new Test ClassO; para1.se tTes t( new Int eger(1O);Test Class resu lt1 = test1 (para1);Sys tem.ou t.prin tln(para1 = + para1.ge tTes tO);Sys tem.ou t.pri nt ln(resul t1 = + resul t1.ge tTes tO);Test Class para2 = new Test ClassO; para2.se tTes t( new Int eger(
4、1O);Tes tClass resu lt2 = tes t2(para2);Sys tem.ou t.prin tln(para2 = + para2.ge tTes tO);Sys tem.ou t.pri nt ln(resul t2 = + resul t2.ge tTes tO);public Tes tClass tes t1 (Tes tClass t) t = new Test ClassO;t .se tTes t(new Int eger(20); return t;public Tes tClass tes t2(Tes tClass t) t .se tTes t(n
5、ew Int eger(20); return t;class Test Class Int eger test = null;public void set Tes t(In teger i) test = i;public Int eger get Tes t() ret urn test;執(zhí)行后的結(jié)果是:paral = 10resu lt1 = 20para2 = 20resu lt2 = 20為什么會(huì)這樣呢?因?yàn)閠estl想通過(guò)修改參數(shù)的引用來(lái)修改返回值,但是在JAVA中, 參數(shù)的引用是不可修改的,所以paral和result1分別指向不同的空間,結(jié)果也 不一樣。而在test2中,re
6、sult2和para2始終指向同一塊區(qū)域,test2方法修改的 是參數(shù)內(nèi)容,而不是參數(shù)的引用。從上面看來(lái),因?yàn)閰?shù)的引用不可改變,如果理解為“參數(shù)的值就是對(duì)該對(duì)象的 引用”,那么java自然只有值傳遞。二,認(rèn)為是引用傳遞。還是上面的例子,如果在參數(shù)傳遞時(shí)理解為“參數(shù)的值就 是該對(duì)象的內(nèi)容”,那么顯然不是值傳遞,因?yàn)閷?duì)象的內(nèi)容已經(jīng)改變了。認(rèn)為是引用傳遞還有一個(gè)理由,就是java有一個(gè)保留字byvalue,現(xiàn)在的JDK 版本中還沒(méi)有實(shí)現(xiàn)這個(gè)保留字,似乎是在暗示對(duì)這種觀點(diǎn)的支持。(Thereappears to be some support for this view within Sun, si
7、nce one of thereserved but not implemented” keywords is byvalue.)所以說(shuō),對(duì)于原題的結(jié)論,是值傳遞還是引用傳遞并不重要,重要的是要理解“對(duì) 象的內(nèi)容可以在被調(diào)用的方法中改變,但對(duì)象的引用是永遠(yuǎn)不會(huì)改變的?!弊⒔?:下面是在Thinking in Java中的原文:This brings up the terminology issue, which always seems good for an argument.The term is “pass by value, ” and the meaning depends on h
8、ow you perceive the opera tion of the program. The general meaning is that you get a local copy of whatever you re passing, but the real question is how you think about what youre passing. When it comes to the meaning of “pass by value,” t here are two fairly dis tinct camps:Java passes ever yt hing
9、 by value. When youre passing primi ti ves into a met hod, you get a distinct copy of the primi ti ve. When you re passing a handle into a met hod, you get a copy of the handle. Ergo, ever yt hing is pass by value. Of course, the assump tion is that youre always t hinking (and caring) that handles a
10、re being passed, but it seems like the Java design has gone a long way to ward allowing you to ignore (mos t of the ti me) that youre working with a handle. Tha t is, it seems to allow you to t hink of the handle as “the object,” since it implicitly dereferences it whenever you make a met hod call.J
11、ava passes primi ti ves by value (no argumen t t here), but objec ts are passed by reference. This is the world view that the handle is an alias for the object, so you don11hink about passing handles, but instead say “Im passing the objec t.” Since you dont get a local copy of the objec t when you p
12、ass it into a met hod, objec ts are clearly not passed by value. There appears to be some suppor t for t his view wi thin Sun, since one of the “reserved but not implemented” keywords is byvalue. (Theres no knowing, however, whet her that keyword will ever see the ligh t of day.)Having given both camps a good airing and after saying “It depends on how you think of a handle, ” I will attempt to sidest
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶(hù)所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶(hù)上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶(hù)因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2024年度河北省護(hù)師類(lèi)之護(hù)士資格證高分通關(guān)題庫(kù)A4可打印版
- 2025江蘇揚(yáng)州大數(shù)據(jù)集團(tuán)子公司管理人員招聘1人筆試備考題庫(kù)及1套參考答案詳解
- 2025江蘇宿遷市泗陽(yáng)縣招聘鄉(xiāng)村醫(yī)生27人筆試備考題庫(kù)及1套完整答案詳解
- 2025河北叢臺(tái)區(qū)選聘農(nóng)村黨務(wù)(村務(wù))工作者42人筆試備考題庫(kù)及參考答案詳解1套
- 2025年?yáng)|營(yíng)市公務(wù)員考試行測(cè)真題有答案詳解
- 新疆喀什地區(qū)喀什市2024-2025學(xué)年高一下學(xué)期期中質(zhì)量監(jiān)測(cè)物理試卷
- 山西省晉中市2024-2025學(xué)年高一上學(xué)期1月期末調(diào)研測(cè)試數(shù)學(xué)試卷(解析版)
- 山東省濟(jì)南市2024-2025學(xué)年高一上學(xué)期期末數(shù)學(xué)試題(解析版)
- 九師聯(lián)盟2024-2025學(xué)年高二下學(xué)期6月摸底聯(lián)考?xì)v史試題(含答案)
- 中式快餐的美食文化體驗(yàn)指南
- 6期文勘土方施工控制方案
- 世界各國(guó)及其首都英文譯名Excel
- 2024年-2025年農(nóng)作物植保員職業(yè)技能考試題及答案
- 2024秋期國(guó)家開(kāi)放大學(xué)《可編程控制器應(yīng)用實(shí)訓(xùn)》一平臺(tái)在線形考(形成任務(wù)1)試題及答案
- 留置針靜脈穿刺
- 專(zhuān)題12《活板》(真題模擬專(zhuān)練)(原卷版)-2024年中考語(yǔ)文課內(nèi)39篇文言文閱讀
- 2023-2024學(xué)年山東省煙臺(tái)市高一下學(xué)期期中生物試題(解析版)
- 淺談機(jī)械設(shè)計(jì)制造及其自動(dòng)化在飛機(jī)發(fā)動(dòng)機(jī)中的應(yīng)用
- ISOIEC38507-2022信息技術(shù)-IT治理-組織使用人工智能的治理影響(中文版-雷澤佳譯2024)
- 2024年西北工業(yè)大學(xué)附中丘成桐少年班初試數(shù)學(xué)試題真題(含答案詳解)
- 科技考古概論全稿講義
評(píng)論
0/150
提交評(píng)論