java中函数的传递对于基本类型变量来说是传值的,对于对象类型的变量传递的是对象的值引用,但依然是值传递。
这与c++中的引用传递是有区别的。
在corejava第八版的第一卷119页有个很好的例子,如下
/**
* This program demonstrates parameter passing in Java.
* @version 1.00 2000-01-27
* @author Cay Horstmann
*/
public class ParamTest
{
public static void main(String[] args)
{
/*
* Test 1: 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);
/*
* Test 2: Methods can change the state of object parameters
*/
System.out.println("\nTesting tripleSalary:");
Employee harry = new Employee("Harry", 50000);
System.out.println("Before: salary=" + harry.getSalary());
tripleSalary(harry);
System.out.println("After: salary=" + harry.getSalary());
/*
* Test 3: 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 += raise;
}
private String name;
private double salary;
}
显示的结果为
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
Testing swap:
Before: a=Alice
Before: b=Bob
End of method: x=Bob
End of method: y=Alice
After: a=Alice
After: b=Bob
主要是中间的那一段,让以前学过c++的人很容易弄混。但看下下面的图就很容易理解了。
这里的对象的引用参数与c++用的指针变量类似,而不是与引用类似,所以依旧是值传递。
第二个容易把人弄晕的是String类型的对象作为参数传递。
有如下代码
public class Test {
static String Change(String s) {
s = "456";
System.out.println("method:" + s);
return s;
}
static StringBuilder Change(StringBuilder s) {
s.replace(0, s.length(), "456");
System.out.println("method:" + s);
return s;
}
/**
* @param args
*/
public static void main(String[] args) {
String str = new String("123");
String str_ref = str;
System.out.println("before:" + str + "," + str_ref + " " + (str == str_ref));
str_ref = Change(str_ref);
System.out.println("atter:" + str + "," + str_ref + " " + (str == str_ref));
System.out.println("**************************");
// TODO Auto-generated method stub
StringBuilder s = new StringBuilder("123");
StringBuilder s2 = s;
System.out.println("before:" + s + "," + s2 + " " + (s == s2));
s2 = Change(s2);
System.out.println("atter:" + s + "," + s2 + " " + (s == s2));
}
}
输出结果为
before:123,123 true
method:456
atter:123,456 false
**************************
before:123,123 true
method:456
atter:456,456 true
原因是String 类型的对象的值不能改变,当函数中s = "456“时s就重新指向一个新的对象。而StringBuilder类型的对象变量调用replace方法时直接修改引用的对象的值。