首页 > 试题广场 >

指出下列程序运行的结果 public class Ex...

[单选题]
指出下列程序运行的结果
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';
    }
}

  • good and abc
  • good and gbc
  • test ok and abc
  • test ok and gbc
需要注意的就是,虽然string是引用类型数据,但是要注意string的不可变性,在change方法中操作的其实是string变量的一个复制值,不影响原值,而通过数组直接修改索引位置上的数据就相当于直接修改引用指向的地址。随意会导致数组数据发生变化。(个人愚见,欢迎指教)
发表于 2021-06-16 17:06:34 回复(0)