Bulletproof web design - 540 Strings and Characters Chapter 10 Software Engineering
540 Strings and Characters Chapter 10 Software Engineering Observation 10.1 In most cases, it is not necessary to make a copy of an existing String object. String objects are immutable their character contents cannot be changed after they are created. Also, if there are one or more references to a String object (or any object for that matter), the object cannot be reclaimed by the garbage collector. Thus, a String reference cannot be used to modify a String object or to delete a String object from memory as in other programming languages, such as C or C++. Line 27 instantiates a new String object and assigns it to reference s3 using class String s constructor that takes a character array as an argument. The new String object contains a copy of the characters in the array. Line 28 instantiates a new String object and assigns it to reference s4 using class String s constructor that takes a char array and two integers as arguments. The second argument specifies the starting position (the offset) from which characters in the array are copied. The third argument specifies the number of characters (the count) to be copied from the array. The new String object contains a copy of the specified characters in the array. If the offset or the countspecified as arguments result in accessing an element outside the bounds of the character array, a StringIndexOutOfBoundsException is thrown. We discuss exceptions in detail in Chapter 14. Line 29 instantiates a new String object and assigns it to reference s5 using class String s constructor that receives a byte array and two integers as arguments. The second and third arguments specify the offset and count, respectively. The new String object contains a copy of the specified bytes in the array. If the offsetor the count specified as arguments result in accessing an element outside the bounds of the character array, a StringIndexOutOfBoundsException is thrown. Line 30 instantiates a new String object and assigns it to reference s6 using class String s constructor that takes a byte array as an argument. The new String object contains a copy of the bytes in the array. Line 31 instantiates a new String object and assigns it to reference s7 using class String s constructor that receives a StringBuffer as an argument. A String- Buffer is a dynamically resizable and modifiable string. The new String object contains a copy of the characters in the StringBuffer. Line 22 creates a new object of class StringBuffer using the constructor that receives a String argument (in this case “WelcometoJavaProgramming”) and assign the new object to reference buffer. We discuss StringBuffers in detail later in this chapter. The screen capture for the program displays the contents of each String. 10.4 String Methods length, charAtand getChars The application of Fig. 10.2 presents String methods length, charAt and get- Chars, which determine the length of a String, get the character at a specific location in a String and get the entire set of characters in a String, respectively. Line 28 uses String method length to determine the number of characters in Strings1. Like arrays, Strings always know their own size. However, unlike arrays, Strings do not have a length instance variable that specifies the number of elements in a String. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01