代码案例浅析" == "

public static void main(String[] args) {
        String str ="hello";
        String str2 = str;        
        String str3 = "he" + new String("llo"); 
        
        System.out.println(str == new String("hello"));     //==比较内存地址  hello是新建的   故等于false
        System.out.println(str == str2);                    //将str2的引用指向str  因为是同一地址  故true
        System.out.println(str == str3);                    //因为str2中的llo是新申请的内存块,而==判断的是对象的地址而非值,所以不一样。
    }

划重点:  == 比较的是内存中的地址

posted @ 2018-08-24 16:24  闻长歌而知雅意  阅读(128)  评论(0编辑  收藏  举报