从jvm运行数据区分析字符串是否相同

    /**
     *  @author: wsq
     *  @Date: 2020/8/19 18:58
     *  @Description: 从jvm运行数据区分析字符串是否相同
     */
    public static void main(String[] args) {
        String a="abc";
        String b=new String("abc");
        System.out.println(a==b);//打印结果为false, 一个在常量池中,一个在堆里面
        String c=b.intern(); // 只要常量池中有,c就指向常量池中的数据,没有就在堆中新建一个对象
        System.out.println(c==b);//打印结果为false
        System.out.println(c==a);//打印结果为true
    }

 

posted @ 2020-08-19 19:02  码在江湖  阅读(132)  评论(0)    收藏  举报