在CSDN也有人讨论过传值与传引用的问题。再用以下的例子作一测试,
对于对象:
传的都是对象的引用,
因为传过去,对象的hashCode()是不变的,
只有在对象的引用进行了“=”操作,它的hashCode就发生了变化。
所以:
a.append("B"); //a的hashCode不变,
b = a; //b的hashCode发生了改变。
对于基本数据类型:
基本数据类型看不到hashCode,
传过去也要进行赋值操作。
public class Test1 {
public static void main(String[] args) {
StringBuffer a = new StringBuffer("A");
StringBuffer b = new StringBuffer("C");
System.out.println("a defore:" + a.hashCode());
System.out.println("b defore:" + b.hashCode());
f(a, b);
System.out.println("a:" + a);
System.out.println("b:" + b);
System.out.println("a after:" + a.hashCode());
System.out.println("b after:" + b.hashCode());
}
public static void f(StringBuffer a, StringBuffer b) {
a.append("B");
b = a;
System.out.println("a middle:" + a.hashCode());
System.out.println("b middle:" + b.hashCode());
}
}