进来看看,你也会很疑惑的。
class Test2 {
String a = "1";
String b = "2";
}
public class NoUpdate {
public static void changeUpd(Test2 t) {
t.a = "abc";
t.b = "123";
}
public static void main(String[] args) {
Test2 t2 = new Test2();
changeUpd(t2);
System.out.println(t2.a + "----" + t2.b);
}
}
这个程序输出 abc----123 ;
但是如果修改一下,
publicclass Test1 {
publicstaticvoid changeStr(String str){
str="welcome";
}
public static void main(String[] args) {
String str="1234";
changeStr(str);
System.out.println(str);
}
}
这里就输出 1234
同样传递的是对象,为什么第二个程序的值没有被修改呢?两个程序传递的参数都应该是原对象的引用的,为什么?想不明白,呵呵