程序代码:基本数据类型传递值,引用数据类型传递地址 ,问为什么String对象却得到这样的结果?求大神解释一下?
public class test001 {
public static void main(String[] args) {
String s = new String("abc") ;
test03(s); //引用数据类型传递地址值
System.out.println("函数外s="+s);
A a = new A () ;
a.setA(3);
dest04(a);
System.out.println("函数外a="+a.getA());
}
private static void dest04(A b) {
b.setA(9);
System.out.println("函数内a="+b.getA());
}
private static void test03(String str) {
str = str+"def" ;
System.out.println("函数内str="+str);// 7
}
}
class A{
private int a ;
public int getA() {
return a;
}
public void setA(int a) {
this.a = a;
}
}
运行结果:
函数内str=abcdef
函数外s=abc
函数内a=9
函数外a=9
疑问:string s 是一个对象,s 存储的是地址值,将地址传递给函数形参,为什么函数对地址数据变化之后外面的String对象值没有变化??
|
|