一道及易掉坑常见的关于值传递的面试题

题目:

public class Example {
     
        String srt = new String("good");
        char[] ch = {
     'a','b','c'};
     public static void main(String[] args){
     
         Example ex = new Example();
         ex.change(ex.srt, ex.ch);
         System.out.print(ex.srt + " and ");
         System.out.print(ex.ch);
    }

    private void change(String str, char[] ch) {
     
            str = "test ok";
            ch[0] = 'g';
    }
}

问题:上面的程序运行的结果是?
A:good and abc
B:good and gbc
C:test ok and abc
D:test ok and gbc

一道及易掉坑常见的关于值传递的面试题_第1张图片
一道及易掉坑常见的关于值传递的面试题_第2张图片
一道及易掉坑常见的关于值传递的面试题_第3张图片

解析:

实际上这道题考察的是值传递的问题,我画个图,大家就一目了然了
一道及易掉坑常见的关于值传递的面试题_第4张图片

结果验证:

一道及易掉坑常见的关于值传递的面试题_第5张图片

答案:B

都看到这里了,不考虑点个赞再走嘛
一道及易掉坑常见的关于值传递的面试题_第6张图片

你可能感兴趣的:(java面试题,面试,java,jvm,字符串,堆栈)