call by value 与call by reference的区别

Java中call by value和call by reference的区别?
2009-07-06 16:06

The term call by value means that the method gets just the value that the caller provides. In contrast, call by reference means that the method gets the location of the variable that the caller provides. Thus, a method can modify the value stored in a variable that is passed by reference but not in one that is passed by value.

public static void tripleValue(double x) // doesn't work { x = 3 * x; }

Call this method:

double percent = 10; tripleValue(percent); It dosen't work.The value of percent is still 10.The reason is :
  1. x is initialized with a copy of the value of percent (that is, 10).

  2. x is tripled—it is now 30. But percent is still 10 .

    3.The method ends, and the parameter variable x is no longer in use.

There are, however, two kinds of method parameters:
  • Primitive types (numbers, Boolean values)

  • Object references

You have seen that it is impossible for a method to change a primitive type parameter. The situation is different for object parameters. You can easily implement a method that triples the salary of an employee: public static void tripleSalary(Employee x) // works { x.raiseSalary(200); } When you call harry = new Employee(. . .); tripleSalary(harry); then the following happens:
  1. x is initialized with a copy of the value of harry, that is, an object reference.

  2. The raiseSalary method is applied to that object reference. The Employee object to which both x and harry refer gets its salary raised by 200 percent.

  3. The method ends, and the parameter variable x is no longer in use. Of course, the object variable harry continues to refer to the object whose salary was tripled.

As you have seen, it is easily possible—and in fact very common—to implement methods that change the state of an object parameter. The reason is simple. The method gets a copy of the object reference, and both the original and the copy refer to the same object. 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. public static void swap(Employee x, Employee y) // doesn't work { Employee temp = x; x = y; y = temp; }

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.

     

    ————————————————————————————————————————————

     

    call by value:

    public   class    CallByValue   {  
          public   static   void   main(String[]   args)   {  
    int   n1   ;  
    int   n2   ;  
    n1   =   9;  
    n2 =   47;  
    System.out.println( "1:   n1=   "   +   n1   +   ",   n2=   "   +   n2);  
    n1   =   n2;  
    System.out.println( "2:   n1=   "   +   n1   +   ",   n2=   "   +   n2);  
    n1   =   27;  
    System.out.println( "3:   n1=   "   +   n1   +   ",   n2=   "   +   n2);
            }  
    }

    result:

    call by reference :

    class   Number   {  
          int   i;  
    }  

    public   class   CallByReference{  
          public   static   void   main(String[]   args)   {  
    Number   n1   =   new   Number();  
    Number   n2   =   new   Number();  
    n1.i   =   9;  
    n2.i   =   47;  
    System.out.println( "1:   n1.i=   "   +   n1.i   +   ",   n2.i=   "   +   n2.i);  
    n1   =   n2;  
    System.out.println( "2:   n1.i=   "   +   n1.i   +   ",   n2.i=   "   +   n2.i);  
    n1.i   =   27;  
    System.out.println( "3:   n1.i=   "   +   n1.i   +   ",   n2.i=   "   +   n2.i);  
            }  
    }

    result:

     

     

    这个例子说明了java中call by value 与call by reference的区别

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值