java吧 关注:1,303,346贴子:12,858,835
  • 2回复贴,共1
public class Example{
String str = new String("good");
char[] ch = {'a','b','c'};
public static void main(String args[]){
Example ex = new Example();
ex.change(ex.str,ex.ch);
System.out.print(ex.str + " and ");
System.out.print(ex.ch);
}
public void change(String str,char ch[]){
str = "test ok";
ch[0] = 'g';
}
}
为什么运行结果str为“good”?


1楼2011-10-04 22:26回复
    String的话,参数传进来的是一个新的,指向原来str的引用,修改这个引用不对原来引用产生影响- -话说修改char[]的话,应该是回变的吧


    IP属地:广东2楼2011-10-04 22:42
    回复
      2026-05-24 06:01:35
      广告
      不感兴趣
      开通SVIP免广告
      二楼正解~


      3楼2011-10-04 23:14
      回复