`

JAVA面试题解惑系列(五)——传了值还是传了引用?

阅读更多
removed.

请到博文下载PDF文件:http://zangweiren.iteye.com/blog/241218
57
13
分享到:
评论
70 楼 朱海宽 2010-10-12  
很想看你的文章,为什么都是removed呢?
在哪里还能看到该文章呢?
69 楼 carfily 2009-06-05  
受益匪浅啊,希望楼主能把解惑系列继续下去
68 楼 flyspider 2009-05-10  
如果真的是传统意义上的引用传递,即ByRef,那么博主给出的例子中引用在方法中被重新赋值的运行结果就不是那样子的了!
JAVA中没有真正的引用传递,传递的全部都是参数的副本(即值),这在很多JAVA大师的著作中均有论述.
67 楼 Ronald9 2008-12-07  
我不知道你们是怎么认为的.

如果你学过C,那么应该是一样的

1.实参总是单向将值传递给形参.

不论是传值也好,传地址也好.只能是单向.只能是实参向形参传.

形参永远,也不可能改变实参的值!

包括你的参数是指针类型


2.既然不能改变实参,但是能改变实参所指向变量的值,即:改变指针变量所指向 的变量的值..

在JAVA实际上就是:  不论你是传基本类型数据还是传引用类型数据.都是单向传递. 你不能改变实参的值,但如果是引用 类型.你可以改变引用变量所指向的对象的值...

这不就完了?
66 楼 luzl 2008-10-21  
 // 为方法参数重新赋值  
    public void change(ParamTest t) {  
        ParamTest tmp = new ParamTest();  
        tmp.num = 9;  
        t = tmp;  
    }  

这段比较难理解,为什么值没有修改呢:
t.change(t);  
System.out.println("运算之后:" + t.num); 

为什么还是零,有朋友告诉我是应为
t.change(t);

时便相当于创建了一个新对象ParamTest t,并且这个对象是指向原来的t,但是当
t = tmp; 
执行时便指向了tmp,相当于这样原来的t还是原来的t,所以还是0,是不是这样呢?
65 楼 iron_wang 2008-10-21  
领教了!谢谢!能加深理解,确实不错!
64 楼 rz_wang 2008-08-11  
不错,加深理解
63 楼 H_eaven 2008-08-05  
传引用还是传值?  Bruce Eckel也认为这个问题没什么意义.
看你的理解了.
我是认为传引用了.
一但进入这个话题,就有一些前提了.
A a = new A();
a       : 是一个引用.
new A() : 是一个值.
引用就相当于C,C++里的指针了,只是它很安全,你可以读它,但不能对它++,--操作.它存在栈里.
new A(),是一个值,一个对象,它存在堆里.
对于我们来说,只能操作像 a 这种栈里的引用,不能操作new A()这种堆里的东西.
所以你认为 a 是一个引用,那就是传引用;
你认为a 是一个值,那就是传值.
也就是你只能传等号前边的,不能传等号后边的.

至于:new A().play(),这种样子..
  这个你只是没有显式给他一个引用,但它仍然有一个匿名引用.

其实我想说一句:传的是栈里的东西.
  也别传值,传引用了.呵呵!
62 楼 狂放不羁 2008-08-03  
无论是传什么,都是传栈上的东西,传基本类型的变量的时候,传的是栈上的值,对于引用类型的变量,传的还是栈上的值,其实都是传值,只所以有传引用和传值之说,是因为有其他语言比如C++等的存在导致,JAVA与C++中的引用是完全不同的概念。
最后一句,JAVA中都是传值的,不要把JAVA中的引用和其他语言的引用混在一起,他们是完全不同的东西。
61 楼 peak 2008-07-28  
用clone的方法就是值传递了
60 楼 臧圩人 2008-07-22  
回复spaceflysky@163.com:
请多多关注,多多支持
59 楼 臧圩人 2008-07-22  
回复rabbitbug:
对,大家对于对象作为参数传递时发生的现象是有共识的,只是在概念上持有不同意见
58 楼 spacefly 2008-07-22  
太感谢您了,我写了1年多java了,现在才搞得这么清楚。。。。
57 楼 abcdos 2008-07-22  
56 楼 rabbitbug 2008-07-22  
吵的这么激烈啊
感觉大家可能只对概念有分歧吧
55 楼 臧圩人 2008-07-17  
回复HenryShanley:

你好,谢谢你的建议!

根据你的建议我已经对文章的结尾做了修改,不过我没有改为JAVA只有值传递的观点。
我在客观地表述了两种观点之后,说出了自己的看法,我希望做到在保持自己立场的同时,不至于误导读者。

PS:我很欣赏你的严谨和负责的态度,呵呵。o(∩_∩)o...
54 楼 臧圩人 2008-07-17  
CJSDN版主HenryShanley写道:
Hi, 臧圩人

Can you take all the comments here in this thread into consideration and modify your existing article if possible? (Because there are quite a lot of misleading and confusing concepts related to pass-by-value and pass-by-reference).

I see your efforts of updating this document, but I am still not happy about the outcome. Nonetheless, those concepts need to be explained in a non-ambiguous way. (some context in the original article is still not intuitive for others). After that, I can give you points according to your update.

Thanks,
Jiafan
53 楼 臧圩人 2008-07-17  
回复dorothyle:

你好。事实上,在引用被作为参数传递给一个方法时,在这个方法体内会发生什么情况,我们是有共识的。唯一的争议只在值传递和引用传递的概念上。

我已经根据CJSDN网友HenryShanley的建议对文章的结尾做了调整,欢迎来我的博客查看最新版本:
http://zangweiren.j a v a e y e.com
52 楼 臧圩人 2008-07-17  
51CTO网友dorothyle写道:

I went to your blog and spent two hours studying your new code and read the passages written by other friends in your blog. And i finally understood what you said, the running result. pass-by-value is the key for java programming. Basic data type's can't be changed inside a method. And reference type's attribute's value can be changed inside a method. But the reference will not be changed forever. Whatever basic data type or reference, only the copy of it to be passed to the method. This is my understand, I am waiting for your instruction.
    For now, i am reading a book written by Y.Daniel Liang, named 'Introduction to Java Programming'. I will continue to try my hard to study. But it is really hard to grasp.
    Thank you again!
51 楼 臧圩人 2008-07-17  
CJSDN版主HenryShanley写道:

臧圩人 写道
回复billgacsli & HenryShanley:

关于值传递和引用传递的问题,我认同Bruce Eckel在《Thinking in Java》第三版中的观点:
In the end, it isn’t that important—what is important is that you understand that passing a reference allows the caller’s object to be changed unexpectedly.



Again, Bruce's sentence is very very misleading. I want to put an end to this topic because it is a trivial issue and it is not that important. (The more critical thing is that you understand it)

The below link shows that Java handles Object by reference but passing them by value.

http://www.javaworld.com/javaworld/javaqa/2000-05/03-qa-0526-pass.html

相关推荐

Global site tag (gtag.js) - Google Analytics