public class Itv {
    static String ss = "kkkkkk";
    static String ss1 = new String("kkkkkk");
    public void change(String str, char ch[]){
        str = "kkkkkk";
        System.out.println(str == ss);//true
        System.out.println(str == ss1);//false
        System.out.println("------------------");
        str = new String("kkkkkk");
        System.out.println(str == ss);//false
        //这句其实是废话 System.out.println(str == ss1);//false
        ch[0] = 'J';
    }
    public static void main(String[] args) {
        String str = new String("good");
        String fry = "good";
        System.out.println(str == fry);//false
        System.out.println("############");
        char[] ch = {'t','e','s','t'};
        Itv itv = new Itv();
        itv.change(str,ch);
        System.out.println("-----------------------------------------");
        itv.change(fry,ch);
        System.out.println(str+"---"+ Arrays.toString(ch));//good---[J, e, s, t]
        System.out.println(fry+"---"+ Arrays.toString(ch));//good---[J, e, s, t]
    }
}

根据输出结果得出结论: 传递的的确是引用,在常量池中有了“kkkkkk”就直接指向此字符串的地址,如果没有就在常量池中开辟空间然后把地址给str

 

如有不对,请大佬指正

版权声明:本文为zedh原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/zedh/p/12995487.html