String经典面试题

1、例题1

package demo;

/**
 * @description: demo15
 * @author: liuyang
 * @create: 2021-08-28 18:15
 */
public class Demo15 {
    String str = new String("good");
    char[] ch = {'t', 'e', 's', 't'};

    public void change1(String str, char[] ch) {
        /**
         * 注意这里str就近原则为方法形参中的str,
         * 并非为类对象的str属性
         */
        str = "test ok";
        ch[0] = 'b';
    }

    public void change2(String str, char[] ch) {
        this.str = "test ok";
        ch[0] = 'b';
    }

    public static void main(String[] args) {
        /**
         * 调用change1输出:
         * good
         * best
         *
         * 调用change2输出:
         * test ok
         * best
         */
        Demo15 demo15 = new Demo15();
        demo15.change1(demo15.str, demo15.ch);
        System.out.println(demo15.str);
        System.out.println(demo15.ch);
    }
}

 

posted @ 2021-08-28 18:29  牛牛的自留地  阅读(85)  评论(0编辑  收藏  举报