Java String when splicing compiler optimizations

In the code

public  static  void main (String [] args) { 
        String STR = " STR " ; 
        Final finalString String = " STR " ; 
        
        String str1 = " str01 " ; 
        String str2 = " STR " + " 01 " ; 
        System.err.println ( str1 == str2);
         // rear of JDK1.6, + constant string operations, optimizes at compile directly into a string
         // so str1 str2 and the same reference point in the constant pool address 
        string Str3 = + STR "01 " ; // compiler automatically calls StringBuilder.apend () method to add
         // Although the contents of the same, but the address is not the same 
        System.err.println (str1 == str3); 
        String str4 = finalString + " 01 " ;
         / / Final variable after compilation directly replaced with the corresponding value, so it = str4 "STR" + "01"
         // plus optimizing compiler merges directly into str4 = "str01", with equal str01 
        System. err.println (str1 == str4); 
        
        string str5 = new new string ( " str01 " ) .intern ();
         // Intern method returns the string pool object, equal 
        System.err.println (str1 == str5) ;
        
    }

 

Guess you like

Origin www.cnblogs.com/yuan-zhou/p/12294741.html