Trace Code string s n Wel come to Javal :String String sl new string("Welcome to Java")i Interned string object for Welcome to ja String s2=sl intern()i string s3 ="Welcome to java Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 16 Trace Code String s = "Welcome to Java"; String s1 = new String("Welcome to Java"); String s2 = s1.intern(); String s3 = "Welcome to Java"; : String Interned string object for "Welcome to Java" s
Trace Code String s ="Welcome to Java string String s1= new String("Welcome to Java") Interned string object for Welcome to java String s2=sl intern()i string s3 ="Welcome to java String A string object for WElcome to Java Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 17 Trace Code String s = "Welcome to Java"; String s1 = new String("Welcome to Java"); String s2 = s1.intern(); String s3 = "Welcome to Java"; : String Interned string object for "Welcome to Java" : String A string object for "Welcome to Java" s s1
Trace Code String s ="Welcome to Java string String sl new string("Welcome to Java")i Interned string object for Welcome to java String s2= sl intern( String s3 =Welcome to Java"; String A string object for WElcome to Java Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 18 Trace Code String s = "Welcome to Java"; String s1 = new String("Welcome to Java"); String s2 = s1.intern(); String s3 = "Welcome to Java"; : String Interned string object for "Welcome to Java" : String A string object for "Welcome to Java" s s1 s2
Trace Code String s ="Welcome to Java s2 string String sl new string("Welcome to Java")i Interned string object for Welcome to java String s2=sl intern()i String s3="Welcome to Java"; String A string object for WElcome to Java Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 19 Trace Code String s = "Welcome to Java"; String s1 = new String("Welcome to Java"); String s2 = s1.intern(); String s3 = "Welcome to Java"; : String Interned string object for "Welcome to Java" : String A string object for "Welcome to Java" s s1 s2 s3
String Method intern ● String comparisons sLow operation O Method intern improves this performance o Returns reference to String o Guarantees reference has same contents as original String To create a constant object in the memory (const pool with the same content with the original object. Then it is enough to just compare their references, not concret content Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 20 String Method intern ⚫String comparisons Slow operation Method intern improves this performance ⚫Returns reference to String ⚫Guarantees reference has same contents as original String To create a constant object in the memory(const pool) with the same content with the original object. Then it is enough to just compare their references ,not concret content