值传递(pass by value)是指在调用函数时将实际参数复制一份传递到函数中,这样在函数中如果对参数进行修改,将不会影响到实际参数。
在Java所有的参数传递中,不管是基本类型还是引用类型,都是值传递。
典型测试例子:
public class Test {
public static void main(String[] args) {
Day today = new Day("Monday");
Day tomorrow = new Day("Tuesday");
swap(today,tomorrow);
System.out.println("after swap method today: " + today.getDesc());
System.out.println("after swap method tomorrow: " + tomorrow.getDesc());
}
//swap 方法
public static void swap(Object o1, Object o2){
Object temp = o1;
o1=o2;
o2=temp;
}
}
class Day {
private String desc;
public Day(String desc) {
this.desc = desc;
}
public String getDesc() {
return desc;
}
}
output:
after swap method today: Monday
after swap method tomorrow: Tuesday
但是,对于基本数据类型和引用类型的参数,常常会有看起来不一样的结果,如下
public class Test1 {
public static void main(String[] args) {
int a = 10;
System.out.println("before changeInt method: a = " + a);
changeInt(a);
System.out.println("after changeInt method: a = " + a);
Num num = new Num();
num.setValue(10);
System.out.println("before changeNum method: num.value = " + num.getValue());
changeNum(num);
System.out.println("after changeNum method: num.value = " + num.getValue());
}
public static void changeInt(int a) {
a = 1;
}
public static void changeNum(Num n) {
n.setValue(1);
}
}
class Num {
private int value;
public void setValue(int value) {
this.value = value;
}
public int getValue() {
return value;
}
}
output:
before changeInt method: a = 10
after changeInt method: a = 10
before changeNum method: num.value = 10
after changeNum method: num.value = 1
经过changeNum(Num n)方法后,num的value值改变了,都说了是值传递,为什么会这样?
上述代码
int a = 10;
Num num = new Num();
num.setValue(10);
如图所示:
对于赋值
a = 20;
Num num2 = num;
如图所示:
而方法调用时,参数的传递基本上就是赋值操作
- 方法参数和局部变量在jvm中的储存方法是相同的,都是在栈上开辟空间来储存的,随着进入方法开辟,退出方法回收。
对于上述的changeInt(int a) 方法,被调用时,它的形式参数 a 作为局部变量,会被赋值为实参的值,即:
所以在方法内对a的操作不会影响的实际参数。
对于上述的changeNum(Num n)方法,被调用时,它的形式参数 n 作为局部变量,也会被赋值为实参的值,即:
如上图所示,局部变量n也获得了与实参num一样的值,所以它们都指向了同一个对象,这也造成了在方法内对n所指向的对象作修改时,实参也会变化的结果,但这是值传递而不是引用传递。(tips: 在Java中操作对象,只有通过引用操作这一种途径。某种意义上,Java中是不能直接操作对象的。)
如果changeNum(Num n)方法变为:
public static void changeNum(Num n) {
n = new Num();
n.setValue(1);
}
则表示:
此时,方法内部对n所指向的对象的修改与实际参数无关,所以
output:
before changeInt method: a = 10
after changeInt method: a = 10
before changeNum method: num.value = 10
after changeNum method: num.value = 10
包装类和String
public class Test2 {
public static void main(String[] args) {
Integer a = Integer.valueOf(20);
System.out.println("before changeInt method: a = " + a);
changeInt(a);
System.out.println("after changeInt method: a = " + a);
String str = new String("Computer");
System.out.println("before changeStr method: str is " + str);
changeStr(str);
System.out.println("after changeStr method: str is " + str);
}
public static void changeStr(String string) {
string += " is wonderful!";
}
public static void changeInt(Integer i) {
i += 1;
}
}
output:
before changeInt method: a = 20
after changeInt method: a = 20
before changeStr method: str is Computer
after changeStr method: str is Computer
String 和 Integer 是引用类型,在changeStr(String string) 、changeInt(Integer i)方法中,我们也未改变过引用的指向,而是直接改变它,按照上述例子,实参应该也会改变,但实际没有,为什么?
实际上,包装类以及
String
类的值,都是final
的,所以在执行+
的过程中,都会重新生成一个对象,然后对它赋值。所以即使上述代码没有显式地改变引用的指向,但当它的内容改变时,它已经重新地指向了新的对象了,那么,对实参引用指向的对象也就没有影响了。