Strings are immutable A String object is immutable: its contents cannot be changed Does the following code change the contents of the string? String s= Java S=HTML iang, Introduction to Java Programming, Sixth Edition, (c)2005 Pearson Education, Inc. All rights reserved. 0-13-148952-6
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved. 0-13-148952-6 6 Strings Are Immutable A String object is immutable; its contents cannot be changed. Does the following code change the contents of the string? String s = "Java"; s = "HTML";
anIMation Trace Code String s= Java S=HTML After executing String s="Java"; S string String object for"Java" Contents cannot be changed Liang, Introduction to Java Programming, Sixth Edition, (c)2005 Pearson Education, Inc. All rights reserved. 0-13-148952-6
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved. 0-13-148952-6 7 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 animation
anIMation Trace Code Strings =Java " HTML After executing s ="HTM String This string object is now unreferenced String object for"Java String String object for"HTML iang, Introduction to Java Programming, Sixth Edition, (c)2005 Pearson Education, Inc. All rights reserved. 0-13-148952-6
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved. 0-13-148952-6 8 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 animation
Interned Strings Since strings are immutable and are frequentl used, to improve efficiency and save memory, the JVM uses a unique instance for string literals with the same character sequence. Such an instance is called interned. You can also use a string objects intern method to return an interned string For example, the following statements Liang, Introduction to Java Programming, Sixth Edition, (c)2005 Pearson Education, Inc. All rights reserved. 0-13-148952-6
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved. 0-13-148952-6 9 Interned Strings Since strings are immutable and are frequently used, to improve efficiency and save memory, the JVM uses a unique instance for string literals with the same character sequence. Such an instance is called interned. You can also use a String object’s intern method to return an interned string. For example, the following statements:
Ex xamples String s ="Welcome to Java String String s1 new String("Welcome to Java")i Interned string object for Welcome to java String s2 s. intern String s3 =Welcome to Javai String System. out. printin ("s1 ==s is +(s1== s) A string object for System. out. printIn("s2==s is +(s2== s) WElcome to Java ystem out println( s3 is +( s3)); display a new object is created if you use the new operator s Is false 02==s is true If you use the string initializer, no new object is created if the interned object is 6 3 is true already created iang, Introduction to Java Programming, Sixth Edition, (c)2005 Pearson Education, Inc. All rights reserved. 0-13-148952-6
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved. 0-13-148952-6 10 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 = s.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