java参数传递时到底是值传递还是引用传递_第1页
java参数传递时到底是值传递还是引用传递_第2页
java参数传递时到底是值传递还是引用传递_第3页
java参数传递时到底是值传递还是引用传递_第4页
java参数传递时到底是值传递还是引用传递_第5页
已阅读5页,还剩2页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、最近比较长一段时间以来,网上的IT同行里面比较流行“ JAVA面试32问”,很 多人的BLOG里都引用这些面试题,最近因为工作内容比较枯燥,也来看看这 些试题以调节一下口味,其中有一道题让我很费解。原题是:当一个对象被当作参数传递到一个方法后,此方法可改变这个对象的属性,并可返回变化后的结果,那么这里到底是值传递还是引用传递?用google查询结果,得到答案基本上是:值传递。当时觉得挺纳闷儿,为什么 连参数的内容都被修改了,怎么还能说是“值传递”呢?因为在传统的印象里(尤其是从C+过来以后),值传递都应该是不改变原参数的。问问周围的同事,也大都这么讲,但是也都讲不清这种理论的根源是什么。 我这

2、 个人有个毛病,有事情想不通时就会憋得难受,后来在 Thin ki ng in Java的 一段内容(注解1)里找到了自己的结论,我认为(Thinking in Java的作 者也这么认为):可以说是值传递,也可以说是引用传递。一,认为是值传递。得出这种结论的前提必须是“参数的值就是对该对象的引用, 而不是对象的内容”,这句话可能有些费解,举个例子加以说明。public class Paier public static void main( Stri ng args) Paier paier = new Paier();paier.test();public void test() Test

3、Class paral = new TestClass();paral.setTest (new In teger(10);TestClass result1 = test1(para1);System.out.pri ntln (para1 = + para1.getTest();System.out.pri ntl n( result1 = + result1.getTest();TestClass para2 = new TestClass();para2.setTest (new In teger(10);TestClass result2 = test2(para2);System.

4、out.pri ntln (para2 = + para2.getTest();System.out.pri ntl n( result2 = + result2.getTest();public TestClass test1(TestClass t) t = new TestClass();t.setTest( new In teger(20);return t;public TestClass test2(TestClass t) t.setTest( new In teger(20);return t;class TestClass In teger test = n ull;publ

5、ic void setTest(I nteger i) test = i;public In teger getTest() return test;执行后的结果是:paral = 10resultl = 20para2 = 20result2 = 20为什么会这样呢?因为testl想通过修改参数的引用来修改返回值,但是在JAVA中, 参数的引用是不可修改的,所以 paral和resultl分别指向不同的空间,结果也 不一样。而在test2中,result2和para2始终指向同一块区域,test2方法修改的 是参数内容,而不是参数的引用。从上面看来,因为参数的引用不可改变,如果理解为“参数的

6、值就是对该对象的 引用”,那么java自然只有值传递。二,认为是引用传递。还是上面的例子,如果在参数传递时理解为“参数的值就 是该对象的内容”,那么显然不是值传递,因为对象的内容已经改变了。认为是引用传递还有一个理由,就是java有一个保留字byvalue,现在的JDK版本中还没有实现这个保留字,似乎是在暗示对这种观点的支持。(Thereappears to be some support for this view within Sun, since one of the“eserved but not implemented ” keywords is byvalue .)所以说,对于原题

7、的结论,是值传递还是引用传递并不重要,重要的是要理解“对 象的内容可以在被调用的方法中改变,但对象的引用是永远不会改变的。”注解1:下面是在Th inking 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 how you perceive the operati on of the program. The gen eral meaning is t

8、hat you get a local copy of whatever youe pass ing, but the real questi on is howyou think about what you re passing. When it comes to the meaning of pass by value, ” there are two fairly distinet camps:1. Java passes everyth ing by value. Whe n youre pass ing primitives intoa method, you get a dist

9、i net copy of the primitive. When youre pass ing ahan dle into a method, you get a copy of the han dle. Ergo, everyth ing is passby value. Of course, the assumpti on is that youre always thi nki ng (andcari ng) that han dles are being passed, but it seems like the Java desig n has gone a long way to

10、ward allowi ng you to ignore (most of the time) that you e working with a handle. That is, it seems to allow you to think of the handle as the object, ” since it implicitly dereferences it whenever you make a method call.2. Java passes primitives by value (no argume nt there), but objects are passed

11、 by refere nee. This is the world view that the han dle is an alias forthe object, so you don think about pass ing han dies, but in stead say“mpassing the object. ” Since you don get a local copy of the object when you pass it into a method, objects are clearly not passed by value. There appears to

12、be some support for this view withi n Sun, since one of thereserved but not implemented” keywords is byvalue . (There no knowing,however, whether that keyword will ever see the light of day.)Having give n both camps a good airi ng and after say ingIt depe nds on howyou think of a han dle, ”1 will attempt to sidestep t

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论