字符串怎样写会被写入常量池

论坛 期权论坛 脚本     
匿名技术用户   2020-12-27 09:40   11   0
String str1 = "iloveu";
    final String str3 = "finalString";
    /*final */String str5 = new String("newfinalString");
    public void demo2(){

        //TODO: 测试字符串怎样写会被写入常量池
        String str2 = new String("iloveu").intern();//"iloveu";//new String("iloveu");
        String str4 = new String("finalString").intern();//"finalString";//new String("finalString");
        String str6 = str5.intern();//"newfinalString";
        String str7 = "sjdljlgew";
        String str8 = new String("sjdljlgew").intern();
        String str9 = "newfinalString";

        println("str1==str2: " + (str1 == str2));
        println("str3==str4: " + (str3==str4));
        println("str5==str6: " + (str5==str6));
        println("str7==str8: " + (str7==str8));
        println("str6==str9: " + (str6==str9));
    }

输出结果:

// System.out: str1==str2: true
// System.out: str3==str4: true
// System.out: str5==str6: false
// System.out: str7==str8: true
// System.out: str6==str9: true

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:7942463
帖子:1588486
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP