T race code Strings= Java S=HTML After executing String s="Java"; After executing s ="HTM S String String This string object is now unreferenced String object for"Java" String object for"Java Contents cannot be changed String String object for"HTML Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 11 Trace Code String s = "Java"; s = "HTML"; : String String object for "Java" s After executing String s = "Java"; After executing s = "HTML"; : String String object for "Java" : String String object for "HTML" Contents cannot be changed This string object is now unreferenced s
T race code Strings= Java S="HTML" After executing String s="Java"; After executing s ="HTM S String String This string object is now unreferenced String object for"Java" String object for"Java Contents cannot be changed String String object for"HTML Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 12 Trace Code String s = "Java"; s = "HTML"; : String String object for "Java" s After executing String s = "Java"; After executing s = "HTML"; : String String object for "Java" : String String object for "HTML" Contents cannot be changed This string object is now unreferenced s
Canonical Strings Since strings are immutable to improve efficiency and save memory, the JVM stores two String objects in the same object if they were created with the same strin literal using the shorthand initializer. Such a string is referred to as a canonical string. You can also use a String object's intern method to return a canonical string, which is the same string that is created using the shorthand initializer Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 13 Canonical Strings Since strings are immutable, to improve efficiency and save memory, the JVM stores two String objects in the same object if they were created with the same string literal using the shorthand initializer. Such a string is referred to as a canonical string. You can also use a String object’s intern method to return a canonical string, which is the same string that is created using the shorthand initializer
Examples String s =Welcome to Java; String Canonical string object String sl new String( Welcome to Java)i for "Welcome to Java String s2= slintern( String s3=Welcome to Javai String string object fo System. out. println ("sl ==s is +(sl ==s))i Welcome to java" System. out. printin(s2==s is +(s2 ==s)) System. out.printin("s==s3 is +(s==$3)) splay 1==s is false s2==s is true s==S3 is true Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 14 Examples display s1 == s is false s2 == s is true s == s3 is true String s = "Welcome to Java"; String s1 = new String("Welcome to Java"); String s2 = s1.intern(); String s3 = "Welcome to Java"; System.out.println("s1 == s is " + (s1 == s)); System.out.println("s2 == s is " + (s2 == s)); System.out.println("s == s3 is " + (s == s3)); : String Canonical string object for "Welcome to Java" : String A string object for "Welcome to Java
Examples string s Welcome to java String String sl new String("Welcome to Java") Interned string object for Welcome to java String s2 sl intern()i String s3 ="Welcome to Java"i S : String System. out. println("sl ==s is +(sl== s A string object for System. out. println(s2==s is +(s2==s)) WElcome to Java System. out. println(s== s3 is + (S== s3) display A new object is created if you use the new operator sI==s is false s2==s is true If you use the string initializer, no new object is created if the interned object is alread S≡s3 Is true created Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 15 Examples display s1 == s is false s2 == s is true s == s3 is true A new object is created if you use the new operator. If you use the string initializer, no new object is created if the interned object is already created. String s = "Welcome to Java"; String s1 = new String("Welcome to Java"); String s2 = s1.intern(); String s3 = "Welcome to Java"; System.out.println("s1 == s is " + (s1 == s)); System.out.println("s2 == s is " + (s2 == s)); System.out.println("s == s3 is " + (s == s3)); : String Interned string object for "Welcome to Java" : String A string object for "Welcome to Java" s s1 s2 s3