package TopCode;
public class HelloWorld {
public static void main(String[] args) {
// TODO Auto-generated method stub
String str = new String( "good ");
Integer in = new Integer(1);
String [] ch = new String[]{ "1 ", "2 ", "3 "};
//System.out.println(ch[0]);
HelloWorld xx = new HelloWorld();
xx.change(str,ch);
System.out.println(str);
//System.out.println(in);
System.out.println(ch[0]);
}
public void change(String str,String[] ch){
str = "test ";
ch[0] = "5 ";
}
}
在change()方法里面修改了,String和String 数组的元数后,在Main方法里输出,System.out.println(str)输出的还是good,而ch[0]输出后就是5了,在这里具体传值是怎么传的呀?请大侠能针对这个例子具体的讲解下好吗?
------解决方案--------------------
java和C++不一样,参数不是指针,可能类似于引用那种。
------解决方案--------------------
change(String str,String[] ch)第一个参数类型改为StringBuffer,在函数体里对其值的修改在主函数中也有效。因为StringBuffer是地址传递,String是值传递。