Pass by value 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: Feedback 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 reference into a method, you get a copy of the reference. Ergo, everything is pass by value. Of course, the assumption is that you’re always thinking (and caring) that references 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 reference. That is, it seems to allow you to think of the reference as “the object,” since it implicitly dereferences it whenever you make a method call. Feedback Java passes primitives by value (no argument there), but objects are passed by reference. This is the world view that the reference is an alias for the object, so you don’t think about passing references, 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 at one time, one of the “reserved but not implemented” keywords was byvalue (This will probably never be implemented). Feedback Having given both camps a good airing, and after saying “It depends on how you think of a reference,” I will attempt to sidestep the issue. 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. ----from <<thinking in java 3rd edition>> appendix A
呵呵,楼上的alienbat(死灵巫师)说得都很正确,不过有点... 呵呵 下面的人也批评得对,不过不要再继续了 我转贴一篇文章给你: -------------------------- 对象是传引用的 参数是传值的这两个能够同时成立吗?一个字:是!在java中,你从来没有传递对象,你传递的仅仅是对象的引用!一句话,java是传引用的。然而,当你传递一个参数,那么只有一种参数传递机制:传值!通常,当程序员讨论传值和传引用时,他们是指语言的参数传递机制,c++同时支持这两种机制,因此,以前使用过c++的程序员开始好像不能确定的java是如何传参数的。java语言为了事情变得简单只支持参数传值的机制。 ★java中的变量有两种类型:引用类型和原始类型。当他们被作为参数传递给方法时,他们都是传值的。这是一个非常重要的差别,下面的代码范例将说明这一点。在继续前,我们有必要定义一下传值和传引用。传值意味着当参数被传递给一个方法或者函数时,方法或者函数接收到的是原始值的副本。因此,如果方法或者函数修改了参数,受影响的只是副本,原始值保持不变。关于java中的参数传递的混乱是因为很多java程序员是从c++转变过来的。c++有引用和非引用类型的变量,并且分别是通过传引用和传值的。java语言有原始类型和对象引用,那么,按照逻辑,java对于原始类型使用传值而对引用是传引用的,就像c++一样。毕竟,你会想到如果你正在传递一个引用,那么它一定是传引用的。这是一个很诱惑人的想法,但是是错误的!在c++和java中,当函数的参数不是引用时,你传递的是值得副本(传值)。但是对于引用类型就不同了。在c++中,当参数是引用类型,你传递的是引用或者内存地址(传引用),而在java中,传递一个引用类型的参数的结果只是传递引用的副本(传值)而非引用自身。这是一个非常重要的区别!java不考虑参数的类型,一律传递参数的副本。仍然不信?如果java中是传引用,那么下面的范例中的swap方法将交换他们的参数。因为是传值,因此这个方法不是像期望的那样正常工作。class Swap{public static void main(String args[]) { Integer a, b; int i,j;a = new Integer(10);b = new Integer(50); i = 5;j = 9; System.out.println("Before Swap, a is " + a); System.out.println("Before Swap, b is " + b); swap(a, b); System.out.println("After Swap a is " + a); System.out.println("After Swap b is " + b); System.out.println("Before Swap i is " + i); System.out.println("Before Swap j is " + j); swap(i,j); System.out.println("After Swap i is " + i);System.out.println("After Swap j is " + j); }public static void swap(Integer ia, Integer ib){Integer temp = ia; ia = ib; ib = temp; }public static void swap(int li, int lj) { int temp = li; li = lj; lj = temp; } } 上面程序的输出是: Before Swap, a is 10 Before Swap, b is 50After Swap a is 10 After Swap b is 50 Before Swap i is 5 Before Swap j is 9 After Swap i is 5 After Swap j is 9 因为swap方法接收到的是引用参数的副本(传值),对他们的修改不会反射到调用代码。译者注:在传递引用和原始类型时还是有不同的,考虑以下的代码:class Change{public static void main(String args[]) { StringBuffer a=new StringBuffer("ok"); int i;i = 5;System.out.println("Before change, a is " + a); change(a); System.out.println("After change a is " + a); System.out.println("Before change i is " + i); change(i); System.out.println("After change i is " + i);}public static void change(StringBuffer ia){ia.append(" ok?");}public static void change(int li) { li = 10; } }程序的输出为:Before change, a is okAfter change a is ok ok?Before change i is 5After change i is 5即如果传递的是引用,那么可以修改引用对象的内容,这个改变会影响到原来的对象,而传递的如果是原始类型则不会有影响。这个也是造成误解的原因之一吧。 -------------------------------------- 这里还有一个url,看了之后,你在这方面就可以给别人当老师了。 http://www.csdn.net/develop/Read_Article.asp?Id=22025
对于基本类型变量(int,long,double,float,byte,boolean,char)是传值。
对于一切对象型变量都是传引用。其实传引用的实质就是传地址,只不过屏蔽了C里面的*和&。绝对需要注意的的是:String类型也是对象型变量,所以它也是传引用。顺便发牢骚。不要以为那些争吵传引用还是传值的家伙是“大虾”,都是一帮垃圾程序员。java写了那么长时间连这最基本的概念还要争论,可见都没读过几年书。
可以说都是传值,但要明确这个值得含义。对于对象,传递的值是对象的指针,并未建立对象的副本,只是建立的指针的副本。
顺便发牢骚。不要以为那些争吵传引用还是传值的家伙是“大虾”,都是一帮垃圾程序员。java写了那么长时间连这最基本的概念还要争论,可见都没读过几年书。说话请留点分寸,不要搬石头砸自己。
继续上面所讲的,比如一个很简单的冒泡算法,100个整数排序,JAVA里如何方便的实现。谁有空写个代码让我学习学习。
不过 对于hogcheng(老驴)说的传值和传引用都可以说是传值
在下认为似乎有点钻牛角尖了
传值和传引用本来就是两个不同的东西,没必要把两者弄在一起,
弄在一起反而更不易理解
还有:
Sting 传的是引用以上只是本人的个人看法
再有: 不要进行人身攻击哟
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: Feedback
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 reference into a method, you get a copy of the reference. Ergo, everything is pass by value. Of course, the assumption is that you’re always thinking (and caring) that references 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 reference. That is, it seems to allow you to think of the reference as “the object,” since it implicitly dereferences it whenever you make a method call. Feedback
Java passes primitives by value (no argument there), but objects are passed by reference. This is the world view that the reference is an alias for the object, so you don’t think about passing references, 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 at one time, one of the “reserved but not implemented” keywords was byvalue (This will probably never be implemented). Feedback
Having given both camps a good airing, and after saying “It depends on how you think of a reference,” I will attempt to sidestep the issue. 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.
----from <<thinking in java 3rd edition>> appendix A
下面的人也批评得对,不过不要再继续了
我转贴一篇文章给你:
--------------------------
对象是传引用的 参数是传值的这两个能够同时成立吗?一个字:是!在java中,你从来没有传递对象,你传递的仅仅是对象的引用!一句话,java是传引用的。然而,当你传递一个参数,那么只有一种参数传递机制:传值!通常,当程序员讨论传值和传引用时,他们是指语言的参数传递机制,c++同时支持这两种机制,因此,以前使用过c++的程序员开始好像不能确定的java是如何传参数的。java语言为了事情变得简单只支持参数传值的机制。 ★java中的变量有两种类型:引用类型和原始类型。当他们被作为参数传递给方法时,他们都是传值的。这是一个非常重要的差别,下面的代码范例将说明这一点。在继续前,我们有必要定义一下传值和传引用。传值意味着当参数被传递给一个方法或者函数时,方法或者函数接收到的是原始值的副本。因此,如果方法或者函数修改了参数,受影响的只是副本,原始值保持不变。关于java中的参数传递的混乱是因为很多java程序员是从c++转变过来的。c++有引用和非引用类型的变量,并且分别是通过传引用和传值的。java语言有原始类型和对象引用,那么,按照逻辑,java对于原始类型使用传值而对引用是传引用的,就像c++一样。毕竟,你会想到如果你正在传递一个引用,那么它一定是传引用的。这是一个很诱惑人的想法,但是是错误的!在c++和java中,当函数的参数不是引用时,你传递的是值得副本(传值)。但是对于引用类型就不同了。在c++中,当参数是引用类型,你传递的是引用或者内存地址(传引用),而在java中,传递一个引用类型的参数的结果只是传递引用的副本(传值)而非引用自身。这是一个非常重要的区别!java不考虑参数的类型,一律传递参数的副本。仍然不信?如果java中是传引用,那么下面的范例中的swap方法将交换他们的参数。因为是传值,因此这个方法不是像期望的那样正常工作。class Swap{public static void main(String args[]) { Integer a, b; int i,j;a = new Integer(10);b = new Integer(50); i = 5;j = 9; System.out.println("Before Swap, a is " + a); System.out.println("Before Swap, b is " + b); swap(a, b); System.out.println("After Swap a is " + a); System.out.println("After Swap b is " + b); System.out.println("Before Swap i is " + i); System.out.println("Before Swap j is " + j); swap(i,j); System.out.println("After Swap i is " + i);System.out.println("After Swap j is " + j); }public static void swap(Integer ia, Integer ib){Integer temp = ia; ia = ib; ib = temp; }public static void swap(int li, int lj) { int temp = li; li = lj; lj = temp; } } 上面程序的输出是: Before Swap, a is 10 Before Swap, b is 50After Swap a is 10 After Swap b is 50 Before Swap i is 5 Before Swap j is 9 After Swap i is 5 After Swap j is 9 因为swap方法接收到的是引用参数的副本(传值),对他们的修改不会反射到调用代码。译者注:在传递引用和原始类型时还是有不同的,考虑以下的代码:class Change{public static void main(String args[]) { StringBuffer a=new StringBuffer("ok"); int i;i = 5;System.out.println("Before change, a is " + a); change(a); System.out.println("After change a is " + a); System.out.println("Before change i is " + i); change(i); System.out.println("After change i is " + i);}public static void change(StringBuffer ia){ia.append(" ok?");}public static void change(int li) { li = 10; } }程序的输出为:Before change, a is okAfter change a is ok ok?Before change i is 5After change i is 5即如果传递的是引用,那么可以修改引用对象的内容,这个改变会影响到原来的对象,而传递的如果是原始类型则不会有影响。这个也是造成误解的原因之一吧。
--------------------------------------
这里还有一个url,看了之后,你在这方面就可以给别人当老师了。
http://www.csdn.net/develop/Read_Article.asp?Id=22025
还有楼上的人身攻击的我不想跟你吵,垃圾程序员是我最鄙视的了。我看你是啥叫“引用”都弄不清楚。没读过书吧?建议你看看《practical java》英文版,中文版容易有歧义。不过只怕你是看不懂英语。