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);
    }
}

 

String经典面试题

上一篇:mr_map


下一篇:2021.8.27北高暑训