java参数传递时到底是值传递还是引用传递(baidu搜集)
最近比较长一段时间以来,网上的IT同行里面比较流行“JAVA面试32问”,很多人的BLOG里都引用这些面试题,最近因为工作内容比较枯燥,也来看看这些试题以调节一下口味,其中有一道题让我很费解。
原题是:当一个对象被当作参数传递到一个方法后,此方法可改变这个对象的属性,并可返回变化后的结果,那么这里到底是值传递还是引用传递?
用google查询结果,得到答案基本上是:值传递。当时觉得挺纳闷儿,为什么连参数的内容都被修改了,怎么还能说是“值传递”呢?因为在传统的印象里(尤其是从C++过来以后),值传递都应该是不改变原参数的。
问问周围的同事,也大都这么讲,但是也都讲不清这种理论的根源是什么。我这个人有个毛病,有事情想不通时就会憋得难受,后来在《Thinking in Java》的一段内容(注解[1])里找到了自己的结论,我认为(《Thinking in Java》的作者也这么认为):可以说是值传递,也可以说是引用传递。
一,认为是值传递。得出这种结论的前提必须是“参数的值就是对该对象的引用,而不是对象的内容”,这句话可能有些费解,举个例子加以说明。
public class Paier {
public static void main(String[] args) {
Paier paier = new Paier();
paier.test();
}
public void test() {
TestClass para1 = new TestClass();
para1.setTest(new Integer(10));
TestClass result1 = test1(para1);
System.out.println("para1 = " + para1.getTest());
System.out.println("result1 = " + result1.getTest());
TestClass para2 = new TestClass();
para2.setTest(new Integer(10));
TestClass result2 = test2(para2);
System.out.println("para2 = " + para2.getTest());
System.out.println("result2 = " + result2.getTest());
}
public TestClass test1(TestClass t) {
t = new TestClass();
t.setTest(new Integer(20));
return t;
}
public TestClass test2(TestClass t) {
t.setTest(new Integer(20));
return t;
}
class TestClass {
Integer test = null;
public void setTest(Integer i) {
test = i;
}
public Integer getTest() {
return test;
}
}
}
执行后的结果是:
para1 = 10
result1 = 20
para2 = 20
result2 = 20
为什么会这样呢?因为test1想通过修改参数的引用来修改返回值,但是在JAVA中,参数的引用是不可修改的,所以para1和result1分别指向不同的空间,结果也不一样。而在test2中,result2和para2始终指向同一块区域,test2方法修改的是参数内容,而不是参数的引用。
从上面看来,因为参数的引用不可改变,如果理解为“参数的值就是对该对象的引用”,那么java自然只有值传递。
二,认为是引用传递。还是上面的例子,如果在参数传递时理解为“参数的值就是该对象的内容”,那么显然不是值传递,因为对象的内容已经改变了。
认为是引用传递还有一个理由,就是java有一个保留字byvalue,现在的JDK版本中还没有实现这个保留字,似乎是在暗示对这种观点的支持。(There appears to be some support for this view within Sun, since one of the “reserved but not implemented” keywords is byvalue.)
所以说,对于原题的结论,是值传递还是引用传递并不重要,重要的是要理解“对象的内容可以在被调用的方法中改变,但对象的引用是永远不会改变的。”
注解[1]:下面是在《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 how you perceive the operation 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 you’re passing. When it comes to the meaning of “pass by value,” there are two fairly distinct camps:
1. Java passes everything by value. When you’re passing primitives into a method, you get a distinct copy of the primitive. When you’re passing a handle into a method, you get a copy of the handle. Ergo, everything is pass by value. Of course, the assumption is that you’re always thinking (and caring) that handles are being passed, but it seems like the Java design has gone a long way toward allowing you to ignore (most of the time) that you’re 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 argument there), but objects are passed by reference. This is the world view that the handle is an alias for the object, so you don’t think about passing handles, but instead say “I’m passing the object.” Since you don’t get a local copy of the object when you pass it into a method, objects are clearly not passed by value. There appears to be some support for this view within Sun, since one of the “reserved but not implemented” keywords is byvalue. (There’s no knowing, however, whether that keyword will ever see the light 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 sidestep the issue for the rest of the book. In the end, it isn’t thatimportant – what is important is that you understand that passing a handle allows the caller’s object to be changed unexpectedly.
文章二
转自:http://www.jiehoo.com/java-pass-parameter.htm
Java参数传递方式
1、对于原始数据类型,也就是int、 long、char之类的类型,是传值的,如果你在方法中修改了值,方法调用结束后,那个变量的值没用改变。
2、对于对象类型,也就是Object的子类,如果你在方法中修改了它的成员的值,那个修改是生效的,方法调用结束后,它的成员是新的值,但是如果你把它指向一个其它的对象,方法调用结束后,原来对它的引用并没用指向新的对象。
public static void main(String[] args) {
changePrimitive(primitive);
MyClass myClass = new MyClass();
//myClass仍然指向的是执行changeObject之前的那个对象
public static void changePrimitive(int primitive) {
public static void changeObject(MyClass myClass) {
对于远程调用,无论是什么类型,调用结束后,传入的参数和以前没用任何变化(当然前途是直接调用远程方法,如果中间经过其它的Proxy类或者Facade类,不能保证那些类对对象没用修改)。至于是通过Locale接口进行调用的,我不太清楚是否属于远程调用。以后确定了再来更新。
相关推荐
java基础面试题方法的参数传递机制本资源系百度网盘分享地址
仅仅用于初学者交流,意在与自动化测试初学者进行相互学习交流,简单的实现了java+selenium+testng参数化打开百度搜索
使用 JavaScript 文件特性,给 JavaScript 传递参数,原创资源,请各位朋友积极点评,谢谢!
java实现对象转化为get请求的param参数String字符串,本人自测非常好用,一般对象可以转化键值对的格式,但是最近项目需要自己手动拼接对象作为get请求的参数,百度了一堆还不如自己写一个,欢迎各位使用,有什么...
程序经测试可运行,只是需要你创建百度合成语音应用,将相关的密钥、KEY之类的参数值替换成你自己的才能正常运行;分享的是知识,有些东西不能分享,比如账号,这个你懂的!!!看得懂百度语音合成REST API的帮助...
这是一个百度语音的java开发包,这个包里面可以实现在java代码里面进行语音的识别等应用。
java解析wsdl文档获取方法与参数的工具类,与博客中描述内容相符。我目前把我的资源全部调成了0分,我希望不要因为其他什么原因改变我的资源下载积分,不然以后只能搞百度云等链接了。
百度网盘:张孝祥Java就业培训教程 百度网盘:张孝祥Java就业培训教程 百度网盘:张孝祥Java就业培训教程 百度网盘:张孝祥Java就业培训教程 百度网盘:张孝祥Java就业培训教程 百度网盘:张孝祥Java就业培训教程 ...
百度搜索接口
百度Java笔试题
主要介绍了java获取百度网盘真实下载链接的方法,涉及java针对URL操作及页面分析的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下
最新版本的java11百度云下载链接其中包含win、liniux、mac各种版本
Java调用百度翻译API翻译实例,您可以在上自行修改。此实例默认支持英文翻译成中文,您可以自行修改!(有JSON工具类,可以使用工具类得到中英文内容)
使用前,导入lib文件夹下的包,点击运行就可以了。而且可以利用此程序设计SO-PMI算法的实现。获得百度搜索数的同时得到两个词语的极性
这是用javaSwing模拟的百度相册,功能很简单
JAVA JDK 11软件新增Epsilon 垃圾收集器和lambda 参数的局部变量语法,可以有效的提高开发者的生产效率和Java应用程序的管理。 新的Java编译器API允许从Java应用程序内部对Java源程序进行编译。在编译期间,应用...
百度 搜索部门 java笔试题,小弟我去百度笔试后记录下来的题目,大家分享
当你仔细阅读书籍时,会发现Java中有大量的数学知识,包括:扰动函数、负载因子、拉链寻址、开放寻址、斐波那契(Fibonacci)散列法还有黄金分割点的使用等等。 适合人群 1. 具备一定编程基础,工作1-3年的研发...
有一篇文库特别想下载,但是没有券。刚刚学完java之后,想试试能不能免费下载……实现了,只是是伪的,只做学习用。