这两天看了几个帖子,关于java的参数传递,而且很多人都用pass by value和pass by reference进行解释,但实际上java 只有pass by value一种传值方式.我在回帖的时候也举过例子了.后来看书发现Core Java中早就有这方面的说明,希望大家不要在误解下去.
下面列出CoreJava中的相关说明 :
Many programming languages (in particular, C++ and Pascal) have two methods for parameter passing: call by value and call by reference. Some programmers (and unfortunately even some book authors) claim that the Java programming language uses call by reference for objects. However, that is false. Because this is such a common misunderstanding, it is worth examining a counterexample in detail.Let's try to write a method that swaps two employee objects:public static void swap(Employee x, Employee y) // doesn't work
{
   Employee temp = x;
   x = y;
   y = temp;
}If the Java programming language used call by reference for objects, this method would work:Employee a = new Employee("Alice", . . .);
Employee b = new Employee("Bob", . . .);
swap(a, b);
// does a now refer to Bob, b to Alice?However, the method does not actually change the object references that are stored in the variables a and b. The x and y parameters of the swap method are initialized with copies of these references. The method then proceeds to swap these copies.// x refers to Alice, y to Bob
Employee temp = x;
x = y;
y = temp;
// now x refers to Bob, y to AliceBut ultimately, this is a wasted effort. When the method ends, the parameter variables x and y are abandoned. The original variables a and b still refer to the same objects as they did before the method call.
This discussion demonstrates that the Java programming language does not use call by reference for objects. Instead, object references are passed by value.Here is a summary of what you can and cannot do with method parameters in the Java programming language:A method cannot modify a parameter of primitive type (that is, numbers or Boolean values).A method can change the state of an object parameter.A method cannot make an object parameter refer to a new object.最后,很有必要记住书中的总结:
1.方法不能修改简单类型参数的值,比如参数为int,long...
2.方法可以改变对象参数的状态,这可以通过调用该对象引用相关的方法来实现,比如调用参数的一些set方法.
3.方法不能将对象参数指向新的对象.其实第二点都没问题,第三点才是关键很多人都不注意这个问题.

解决方案 »

  1.   

    此回复为自动发出,仅用于显示而已,并无任何其他特殊作用
    楼主【ZangXT】截止到2008-06-22 16:04:03的历史汇总数据(不包括此帖):
    注册日期:2008-5-21
    上次登录:2008-6-20
    发帖数:2                  发帖分:20                 
    结贴数:1                  结贴分:20                 
    结贴率:50.00%        结分率:100.00%        
    楼主加油
      

  2.   

    就用这样一个例子说java是pass-by-value也不太对吧。
    从下面的代码可以看出,a指向了Alice,而b指向了Bob
    Employee a = new Employee("Alice", . . .);
    Employee b = new Employee("Bob", . . .);
    swap(a, b); 
    调用swap方法后,swap方法里的x,y两个方法内的引用分别也指向原来a,b所引用的对象,而swap方法只是简单的在将方法内的x,y两个引用所指向的对象换了一下位置,当然不会影响到原来a,b两个引用。单单的通过这个例子不能说明java是pass-by-value,如果说通过这个例子说明java是pass by reference也很合适。
      

  3.   

    其实传递的时这个reference的value
      

  4.   

    此回复为自动发出,仅用于显示而已,并无任何其他特殊作用
    楼主【ZangXT】截止到2008-06-22 17:51:42的历史汇总数据(不包括此帖):
    发帖数:2                  发帖分:20                 
    结贴数:1                  结贴分:20                 
    结贴率:50.00 %        结分率:100.00%        
    楼主加油