Topic: 引用传递

  Print this page

1.引用传递 Copy to clipboard
Posted by: skwujinhua
Posted on: 2009-06-15 10:29

java中基本類型實現引用传递怎么弄?

C#中有個關鍵字﹕ref 如﹕int max(ref int a,ref int b)表示a,b都是引用傳過來了﹐java中有沒? java中只有傳對象的時候才是引用传递嗎?

2.Re:引用传递 [Re: skwujinhua] Copy to clipboard
Posted by: skwujinhua
Posted on: 2009-06-15 10:48

難道我就只能把這二個基本類型放在類中才行?
我看是這樣﹐不然有違 "java一切都是對象的原則了。"

3.Re:引用传递 [Re: skwujinhua] Copy to clipboard
Posted by: skwujinhua
Posted on: 2009-06-15 10:56

我現在肯定對基本類型如果要進行引用傳遞的話﹐只能把基本類型封裝在類中﹐通過傳對象的方法實現。用Integer不行﹗

4.Re:引用传递 [Re: skwujinhua] Copy to clipboard
Posted by: abefirst
Posted on: 2009-06-28 11:04

我也不大明白

5.Re:引用传递 [Re: skwujinhua] Copy to clipboard
Posted by: abefirst
Posted on: 2009-06-28 11:06

反正好像是JAVA利通过引用对象实现field的修改。

6.Re:引用传递 [Re: skwujinhua] Copy to clipboard
Posted by: abefirst
Posted on: 2009-06-28 11:15

为什么用Iterger不行啊。他不是封装基本类型的吗

7.Re:引用传递 [Re: skwujinhua] Copy to clipboard
Posted by: skwujinhua
Posted on: 2009-06-29 17:03

--! ,我也沒找到原因﹐為什么Integer不行。

始我也以為用Integer應該可以﹐但后來試了一下﹐定義了二個Integer測試﹐不行﹐網上也找了用Integer不能達到效果。

8.Re:引用传递 [Re: skwujinhua] Copy to clipboard
Posted by: JiafanZhou
Posted on: 2009-06-29 21:11

skwujinhua wrote:
java中基本類型實現引用传递怎么弄?
java中有沒? java中只有傳對象的時候才是引用传递嗎?

No, there is no such a concept in Java as "Pass-by-reference". Everything is "Pass-by-value" in Java. (of course including the Integer)

I will be pretty busy these days, so I have very little time for anybody's questions. But I will answer this one. (FYI: I will finish my SCBCD exam on mid-August, wish me good luck :--) )

Java does manipulate objects by reference, and all object variables are references. However, Java doesn't pass method arguments by reference; it passes them by value.

Consider the following code, read the code comments for explanation:

public class ReferenceTest
{
public void trickyMethod(Integer number)
{
// The number is passed in as a copy of the original reference (i.e. pass by value)
// If it is pass-by-reference, the number will be changed because of the single reference.
number = new Integer(20);
System.out.println("In the method(), integer=" + number);
}

public static void main(String[] args)
{
ReferenceTest refTest = new ReferenceTest();

Integer integer = new Integer(10);
System.out.println("Before trickymethod() integer=" + integer );
refTest.trickyMethod(integer);
System.out.println("After trickymethod() integer=" + integer);
}
}

// the following is the output:
Before trickymethod() integer=10
In the method(), integer=20
After trickymethod() integer=10


Also, this link explains things further in fun:
http://www.javaworld.com/javaworld/javaqa/2000-05/03-qa-0526-pass.html

Regards,
Jiafan

9.Re:引用传递 [Re: skwujinhua] Copy to clipboard
Posted by: JiafanZhou
Posted on: 2009-06-29 21:25

You will also probably be interested in reading this thread:

http://www.cjsdn.net/post/view?bid=1&id=195048&sty=3

10.Re:引用传递 [Re: skwujinhua] Copy to clipboard
Posted by: skwujinhua
Posted on: 2009-06-30 10:01

學習了﹐靈魂深處還是不能一下子轉變過來。

JiafanZhou,感謝你抽時間來回答我的提問﹐祝你考試順利﹗

11.Re:引用传递 [Re: skwujinhua] Copy to clipboard
Posted by: skwujinhua
Posted on: 2009-07-01 14:40

是不是這樣?

直接通過對引用進行賦值操作以改變引用所指的成員變量都不會改變原來的值。而只能通過改變引用所指向的成員變量(即改變屬性值)才能改變原來的值并返回給調用函數。

我看就是這樣了。哈哈。。。。。。。。。


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923