首先回顾一下在程序设计语言中有关将参数传递给方法(或函数)的一些专业术语。值调用(call by value)表示方法接收的是调用者提供的值。而引用调用(call by reference)表示方法接收的是调用者提供的变量地址。一个方法能够修改传递引用所对应的变量值,而不能修改传递值调用所对应的变量值。“...调用”(call by)是一个标注的计算机科学术语,它用来描述各类程序设计语言中方法参数的传递方式(事实上,之前还有称(call by name),Algol程序设计语言是最古老的高级程序设计语言之一,它使用的就是这种参数传递方式。不过,对于今天,这种传递方式已经成为历史)。程序员
Java程序设计语言老是采用值调用。也就是说,方法获得的是全部参数值的一个拷贝,特别是,方法不能修改传递给它的任何参数变量的内容。ide
例如,考虑下面的调用:函数
- double percent = 10;
- harry.raiseSalary(percent);
没必要理睬这个方法的具体实现,在方法调用以后,percent的值仍是10.spa
下面再仔细地研究一下这种状况。假定一个方法试图将一个参数值增长至3倍:设计
- public static void tripleValue(double x) //doesn't work
- {
- x = 3 * x;
- }
而后调用这个方法:对象
- double percent = 10;
- tripleValue(percent);
能够看到,不管怎样,调用这个方法以后,percent的值仍是10。下面看一下具体的执行过程:ip
1)x被初始化为percent值的一个拷贝(也就是10)。get
2)x被乘以3后等于30。可是percent仍然是10。string
3)这个方法结束以后,参数变量x再也不使用。it
然而,方法参数共有两种类型:
读者已经看到,一个方法不可能修改一个基本数据类型的参数。而对象引用做为参数就不一样了,能够很容易地利用下面这个方法实现将一个雇员的薪金提升两倍的操做:
- public static void tripleSalary(Employee x) // works
- {
- x.raiseSalary(200);
- }
当调用
- harry = new Employee(...);
- tripleSalary(harry);
时,具体的执行过程为:
1)x被初始化为harry值的拷贝,这里是一个对象的引用。
2)raiseSalary方法应用于这个对象引用。x和harry同时引用的那个Employee对象的薪金提升了200%。
3)方法结束后,参数变量x再也不使用。固然,对象变量harry继续引用那个薪金增至3倍的雇员对象。
读者已经看到,实现一个改变对象参数状态的方法并非一件难事。理由很简单,方法获得的是对象引用的拷贝,对象引用及其余的拷贝同时引用同一个对象。
不少程序设计语言(特别是,C++和Pascal)提供了两种参数传递的方式:值调用和引用调用。有些程序员(甚至本书的做者)认为Java程序设计语言对对象采用的是引用调用,实际上,这种理解是不对的。因为这种错误具备必定的广泛性,因此下面给出一个反例来详细地阐述一下这个问题。
首先,编写一个交换两个雇员对象的方法:
- public static void swap(Employee x, Employee y)
- {
- Employee temp = x;
- x = y;
- y = temp;
- }
若是Java程序设计语言对对象采用的是引用调用,那么这个方法就应该可以实现交换数据的效果:
- Employee a = new Employee("Alice", ...);
- Employee b = new Employee("Bob", ...);
- swap(a, b);
- // dose a now refer to Bob, b to Alice?
可是,方法并无改变存储在变量a和b中的对象引用。swap方法的参数x和y被初始化为两个对象引用的拷贝,这个方法交换的是这个两个拷贝。
- // x refer to Alice, y to Bob
- Employee temp = x;
- x = y;
- y = temp;
- // now x refer Bob, y to Alice
最终,白费力气。在方法结束时参数变量x和y被丢弃了。原来的变量a和b仍然引用这个方法调用以前所引用的对象。
这个过程说明:Java程序设计语言对对象采用的不是引用调用,实际上,对象引用进行的是值传递。
下面总结一下在Java程序设计语言中,方法参数的使用状况:
例4-4中的程序给出了相应的演示。在这个程序中,首先试图将一个值参数的值提升两倍,可是没有成功:
- Testing tripleValue:
- Before: percent = 10.0
- End of method: x = 30.0
- After: percent = 10.0
随后,成功地将一个雇员的薪金提升了两倍:
- Testing tripleSalary:
- Before: salary = 50000.0
- End of method: salary = 150000.0
- After: salary = 150000.0
方法结束以后,harry引用的对象状态发生了改变。这是由于这个方法能够经过对象引用的拷贝修改所引用对象的状态。
最后,程序演示了swap方法的失败效果:
- Testing swap:
- Before: a = Alice
- Before: b = Bob
- End of method: x = Bob
- End of method: y = Alice
- After: a = Alice
- After: b = Bob
能够看出,参数变量x和y被交换了,可是变量a和b没有收到影响。
C++注释:C++有值调用和引用调用。引用参数标有&符号。例如,能够轻松地实现void tripleValue(double& x)方法或void swap(Employee& x, Employee& y)方法实现修改它们的引用参数的目的。
- /**
- * This program demonstrates parameter passing in Java
- * @version 1.01 2012-11-13
- * @author Wucg
- */
- public class ParamTest
- {
- public static void main(String[] args)
- {
- /*
- * Test1: Methods can't modify numeric parameters
- */
- System.out.println("Testing tripleValue:");
- double percent = 10;
- System.out.println("Before: percent = " + percent);
- tripleValue(percent);
- System.out.println("After: percent = " + percent);
- /*
- * Test2: Methods can change the state of object parameters
- */
- System.out.println("\nTesting tripleSalary:");
- Employee harry = new Employee("Harry", 70000);
- System.out.println("Before: salary = " + harry.getSalary());
- tripleSalary(harry);
- System.out.println("After: salary = " + harry.getSalary());
- /*
- * Test3: Methods can't attach new objects to object parameters
- */
- System.out.println("\nTesting swap:");
- Employee a = new Employee("Alice", 70000);
- Employee b = new Employee("Bob", 60000);
- System.out.println("Before: a = " + a.getName());
- System.out.println("Before: b = " + b.getName());
- swap(a, b);
- System.out.println("After: a = " + a.getName());
- System.out.println("After: b = " + b.getName());
- }
- public static void tripleValue(double x) // doesn't work
- {
- x = 3 * x;
- System.out.println("End of method: x = " + x);
- }
- public static void tripleSalary(Employee x) // works
- {
- x.raiseSalary(200);
- System.out.println("End of method: salary = " + x.getSalary());
- }
- public static void swap(Employee x, Employee y)
- {
- Employee temp = x;
- x = y;
- y = temp;
- System.out.println("End of method: x = " + x.getName());
- System.out.println("End of method: y = " + y.getName());
- }
- }
- class Employee // simplified Employee class
- {
- public Employee(String n, double s)
- {
- name = n;
- salary = s;
- }
- public String getName()
- {
- return name;
- }
- public double getSalary()
- {
- return salary;
- }
- public void raiseSalary(double byPercent)
- {
- double raise = salary * byPercent / 100;
- salary = salary + raise;
- }
- private String name;
- private double salary;
- }