代码案例浅析" == "

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是新申请的内存块,而==判断的是对象的地址而非值,所以不一样。
    }

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

往事如烟,余生有我.
原文地址:https://www.cnblogs.com/assistants/p/9530592.html