| 准确的说  change方法只改变了ch零角标位的值,,代码中改变的str值改变的只是change方法中局部变量str的值,正确的做法是在change方法的str前面加上this,代表使用这个方法的对象引用,比如这样,输出就成了test ok and gbc 复制代码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.println(ex.ch);
        }
        public void change(String str, char ch[])
        {
                this.str = "test ok";
                ch[0] = 'g';
        }
}
 |