Chapter 8 Strings and Text 1/O Chapter 6 Arrays Chapter 7 Objects and Classes Chapter 8 Strings and Text 1/O GUI can be covered after $10.2, "Abstract Classes Chapter 9 Inheritance and Polymorphism CHapter 12 GUI Basics $10.2,Abstract Classes Chapter 13 Graphics §10.4, Interfaces Chapter 14 Event-Driven Pi mIng Chapter 11 Object-Oriented Design Exception and binary 1O can be covered after Chapter 9 Chapter 17 Exceptions and Assertions Chapter 18 Binary 1O 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 1 Chapter 8 Strings and Text I/O Chapter 7 Objects and Classes Chapter 8 Strings and Text I/O Chapter 9 Inheritance and Polymorphism Chapter 6 Arrays §10.2, “Abstract Classes” §10.4, “Interfaces” Chapter 13 Graphics Chapter 14 Event-Driven Programming Chapter 17 Exceptions and Assertions Chapter 18 Binary I/O Exception and binary I/O can be covered after Chapter 9 GUI can be covered after §10.2, “Abstract Classes” Chapter 12 GUI Basics Chapter 11 Object-Oriented Design
Objectives To use the String class to process fixed strings($8.2) To use the Character class to process a single character($8.3) To use the String Builder/string buffer class to process flexible strings(§84 o know the differences between the String, String Builder, and String Buffer classes(882-8.4) To learn how to pass strings to the main method from the command line(§8.5) (Optional) To use the regular expressions to represent patterns for matching, replacing, and splitting strings(88.6) To discover file properties, delete and rename files using the file class(§8.7) To write data to a file using the Print Writer class($8.8.1) To read data from a file using the Scanner class($8.8. 2) 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 2 Objectives To use the String class to process fixed strings (§8.2). To use the Character class to process a single character (§8.3). To use the StringBuilder/StringBuffer class to process flexible strings (§8.4). To know the differences between the String, StringBuilder, and StringBuffer classes (§8.2-8.4). To learn how to pass strings to the main method from the command line (§8.5). (Optional) To use the regular expressions to represent patterns for matching, replacing, and splitting strings (§8.6). To discover file properties, delete and rename files using the File class (§8.7). To write data to a file using the PrintWriter class (§8.8.1). To read data from a file using the Scanner class (§8.8.2)
The string Class Constructing a String String message =Welcome to Java String message new String("Welcome to Java) String s= new string()i Obtaining String length and Retrieving Individual characters in a string String Concatenation(concat Substrings(substring(index), substring(start, end)) Comparisons(equals, compareTo String Conversions Finding a Character or a Substring in a String Conversions between Strings and arrays Converting Characters and Numeric Values to Strings 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 3 The String Class Constructing a String: – String message = "Welcome to Java“; – String message = new String("Welcome to Java“); – String s = new String(); Obtaining String length and Retrieving Individual Characters in a string String Concatenation (concat) Substrings (substring(index), substring(start, end)) Comparisons (equals, compareTo) String Conversions Finding a Character or a Substring in a String Conversions between Strings and Arrays Converting Characters and Numeric Values to Strings
java. lang String instructs an empty string String( value: String) Constructs a string with the specified string literal value Constructs a string with the specified character array harAt(index: int ): char Returns the character at the specified index from this string mare To(anotherString: String ):int ompares this string with another string ompare Tolgnore Case(anotherString: String): int Compares this string with another string ignoring case at(another String: String): String ncat this string with another string nds With(suffix: String): boolean Returns true if this string ends with the specified suffix uals(another String: String): boolean returns true if this string is equal to anther string qualslgnore Case(another String: String): boolean Checks if this string equals anther string ignoring case Chars(int srcBegin, int srcEnd, char[] dst, int Copies characters from this string into the destination character h: int): int Returns the index of the first occurrence of indexOf(ch: int, fromIndex: int) int Returns the index of the first occurrence of ch after fromIndex ndexofstr; String): int eturns the index of the first occurrence of str ndexOfstr: String, fromIndex: int ) int Returns the index of the first occurrence of str after fromlndey lastlndexof(ch: int): int Returns the index of the last occurrence of ch Returns the index of the last occurrence of ch before fromIndex lastIndexof(str: String): int Returns the index of the last occurrence of str lastIndexOf(str: String, fromIndex: int ) int Returns the index of the last occurrence of str before fromlnd region Matches(toffset: int, other: String, offset: s true if the specified subregion of thi int, len: int): boolean matches the specified subregion of the string argur Int Returns the number of characters in this string place(old Char:char,newChar:char): String Returns a new string with old Char replaced by newChar ith(prefix: String ): boolean Returns true if this string starts with the specified prefi ubstring(beginIndex: int): String Returns the substring from beginIndex ubString(beginIndex: int, endIndex: int ) String Returns the substring from beginIndex to endIndex-1 CharArray(: charl Returns a char array consisting characters from this string LowerCase(: String Returns a new string with all characters converted to lowercase Returns a new string with itself Upper Case(): String Returns a new string with all characters converted to uppercase mo: String Returns a string with blank characters trimmed on both sides copy ValueOf(data: charl): String Returns a new string consisting of the char array data alueOf data: char): String Same as copy ValueOfidata: charl): String valueofid: double): String eturns a string representing the double value valueof: float): String Returns a string representing the float value Returns a string representing the int value Returns a string representing the long value
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved. 0-13-148952-6 4 java.lang.String +String() +String(value: String) +String(value: char[]) +charAt(index: int): char +compareTo(anotherString: String): int +compareToIgnoreCase(anotherString: String): int +concat(anotherString: String): String +endsWith(suffix: String): boolean +equals(anotherString: String): boolean +equalsIgnoreCase(anotherString: String): boolean +getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin): void +indexOf(ch: int): int +indexOf(ch: int, fromIndex: int): int +indexOf(str: String): int +indexOf(str: String, fromIndex: int): int +lastIndexOf(ch: int): int +lastIndexOf(ch: int, fromIndex: int): int +lastIndexOf(str: String): int +lastIndexOf(str: String, fromIndex: int): int +regionMatches(toffset: int, other: String, offset: int, len: int): boolean +length(): int +replace(oldChar: char, newChar: char): String +startsWith(prefix: String): boolean +subString(beginIndex: int): String +subString(beginIndex: int, endIndex: int): String +toCharArray(): char[] +toLowerCase(): String +toString(): String +toUpperCase(): String +trim(): String +copyValueOf(data: char[]): String +valueOf(c: char): String +valueOf(data: char[]): String +valueOf(d: double): String +valueOf(f: float): String +valueOf(i: int): String +valueOf(l: long): String Constructs an empty string Constructs a string with the specified string literal value Constructs a string with the specified character array Returns the character at the specified index from this string Compares this string with another string Compares this string with another string ignoring case Concat this string with another string Returns true if this string ends with the specified suffix Returns true if this string is equal to anther string Checks if this string equals anther string ignoring case Copies characters from this string into the destination character array Returns the index of the first occurrence of ch Returns the index of the first occurrence of ch after fromIndex Returns the index of the first occurrence of str Returns the index of the first occurrence of str after fromIndex Returns the index of the last occurrence of ch Returns the index of the last occurrence of ch before fromIndex Returns the index of the last occurrence of str Returns the index of the last occurrence of str before fromIndex Returns true if the specified subregion of this string exactly matches the specified subregion of the string argument Returns the number of characters in this string Returns a new string with oldChar replaced by newChar Returns true if this string starts with the specified prefix Returns the substring from beginIndex Returns the substring from beginIndex to endIndex-1. Returns a char array consisting characters from this string Returns a new string with all characters converted to lowercase Returns a new string with itself Returns a new string with all characters converted to uppercase Returns a string with blank characters trimmed on both sides Returns a new string consisting of the char array data Returns a string consisting of the character c Same as copyValueOf(data: char[]): String Returns a string representing the double value Returns a string representing the float value Returns a string representing the int value Returns a string representing the long value
Constructing Strings String newString=new String(stringLiteral) String message =new String("Welcome to Java") Since strings are used frequently, Java provides a shorthand initializer for creating a string String message =Welcome to Java 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 5 Constructing Strings String newString = new String(stringLiteral); String message = new String("Welcome to Java"); Since strings are used frequently, Java provides a shorthand initializer for creating a string: String message = "Welcome to Java";
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