Archive for November, 2007

562 Strings and Characters Chapter 10 13 StringBuffer (Web hosting contract)

Friday, November 16th, 2007

562 Strings and Characters Chapter 10 13 StringBuffer buffer = 14 new StringBuffer( “Hello, how are you?” ); 15 16 String output = “buffer = ” + buffer.toString() + 17 “nlength = ” + buffer.length() + 18 “ncapacity = ” + buffer.capacity(); 19 20 buffer.ensureCapacity( 75 ); 21 output += “nnNew capacity = ” + buffer.capacity(); 22 23 buffer.setLength( 10 ); 24 output += “nnNew length = ” + buffer.length() + 25 “nbuf = ” + buffer.toString(); 26 27 JOptionPane.showMessageDialog( null, output, 28 “StringBuffer length and capacity Methods”, 29 JOptionPane.INFORMATION_MESSAGE ); 30 31 System.exit( 0 ); 32 } 33 34 } // end class StringBufferCapLen Fig. 10.13 StringBufferlengthand capacitymethods (part 2 of 2). The program contains one StringBuffer called buffer. Lines 13 14 of the program use the StringBufferconstructor that takes a Stringargument to instantiate and initialize the StringBuffer with “Hello, how are you?”. Lines 16 18 append to output the contents, the length and the capacity of the StringBuffer. Notice in the output window that the capacity of the StringBufferis initially 35. Remember that the StringBuffer constructor that takes a String argument creates a StringBuffer object with an initial capacity that is the length of the Stringpassed as an argument plus 16. Line 20 expands the capacity of the StringBufferto a minimum of 75 characters. Actually, if the original capacity is less than the argument, the method ensures a capacity that is the greater of the number specified as an argument or twice the original capacity plus 2. If the StringBuffer s current capacity is more than the specified capacity, the StringBuffer s capacity remains unchanged. Line 23 uses method setLength to set the length of the StringBufferto 10. If the specified length is less than the current number of characters in the StringBuffer, Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

Chapter 10 Strings and Characters 561 14 buffer1

Thursday, November 15th, 2007

Chapter 10 Strings and Characters 561 14 buffer1 = new StringBuffer(); 15 buffer2 = new StringBuffer( 10 ); 16 buffer3 = new StringBuffer( “hello” ); 17 18 String output = 19 “buffer1 = “” + buffer1.toString() + “”" + 20 “nbuffer2 = “” + buffer2.toString() + “”" + 21 “nbuffer3 = “” + buffer3.toString() + “”"; 22 23 JOptionPane.showMessageDialog( null, output, 24 “Demonstrating StringBuffer Class Constructors”, 25 JOptionPane.INFORMATION_MESSAGE ); 26 27 System.exit( 0 ); 28 } 29 30 } // end class StringBufferConstructors Fig. 10.12 StringBufferclass constructors (part 2 of 2). 10.15 StringBufferMethods length, capacity, setLengthand ensureCapacity Class StringBufferprovides the lengthand capacitymethods to return the number of characters currently in a StringBufferand the number of characters that can be stored in a StringBuffer without allocating more memory, respectively. Method ensureCapacity is provided to allow the programmer to guarantee that a String- Buffer has a minimum capacity. Method setLength is provided to enable the programmer to increase or decrease the length of a StringBuffer. The program of Fig. 10.13 demonstrates these methods. 1 // Fig. 10.13: StringBufferCapLen.java 2 // This program demonstrates the length and 3 // capacity methods of the StringBuffer class. 4 5 // Java extension packages 6 import javax.swing.*; 7 8 public class StringBufferCapLen { 9 10 // test StringBuffer methods for capacity and length 11 public static void main( String args[] ) 12 { Fig. 10.13 StringBufferlengthand capacitymethods (part 1 of 2). Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

560 Strings and Characters Chapter 10 ters. As (Web hosting faq)

Thursday, November 15th, 2007

560 Strings and Characters Chapter 10 ters. As we will see, class StringBufferis also used to implement operators +and += for Stringconcatenation. Performance Tip 10.3 String objects are constant strings and StringBuffer objects are modifiable strings. Java distinguishes constant strings from modifiable strings for optimization purposes; in particular, Java can perform certain optimizations involving String objects (such as sharing one String object among multiple references) because it knows these objects will not change. Performance Tip 10.4 When given the choice between using a String object to represent a string versus a StringBuffer object to represent that string, always use a String object if indeed the object will not change; this improves performance. Common Programming Error 10.4 Invoking StringBuffer methods that are not methods of class String on String objects is a syntax error. 10.14 StringBufferConstructors Class StringBufferprovides three constructors (demonstrated in Fig. 10.12). Line 14 uses the default StringBuffer constructor to create a StringBuffer with no characters in it and an initial capacity of 16 characters. Line 15 uses StringBufferconstructor that takes an integer argument to create a StringBufferwith no characters in it and the initial capacity specified in the integer argument (i.e., 10). Line 16 uses the String- Bufferconstructor that takes a Stringargument to create a StringBuffercontaining the characters of the Stringargument. The initial capacity is the number of characters in the Stringargument plus 16. The statement on lines 18 21 uses StringBuffer method toString to convert the StringBuffers into String objects that can be displayed with drawString. Note the use of operator +to concatenate Strings for output. In Section 10.17, we discuss how Java uses StringBuffers to implement the + and += operators for String concatenation. 1 // Fig. 10.12: StringBufferConstructors.java 2 // This program demonstrates the StringBuffer constructors. 3 4 // Java extension packages 5 import javax.swing.*; 6 7 public class StringBufferConstructors { 8 9 // test StringBuffer constructors 10 public static void main( String args[] ) 11 { 12 StringBuffer buffer1, buffer2, buffer3; 13 Fig. 10.12 StringBufferclass constructors (part 1 of 2). Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

Chapter 10 Strings and Characters 559 66 JOptionPane.showMessageDialog(

Wednesday, November 14th, 2007

Chapter 10 Strings and Characters 559 66 JOptionPane.showMessageDialog( null, output, 67 “Demonstrating String Method intern”, 68 JOptionPane.INFORMATION_MESSAGE ); 69 70 System.exit( 0 ); 71 } 72 73 } // end class StringIntern Fig. 10.11 Stringclass internmethod (part 3 of 3). Line 33 uses method internto get a reference to a Stringwith the same contents as object s1and assigns the reference to s3. The Stringto which s3 refers is maintained in memory by class String. Line 34 also uses method intern to get a reference to a Stringobject. However, because Strings1and Strings2have the same contents, the reference returned by this call to intern is a reference to the same String object returned by s1.intern(). The third if structure (lines 38 42) uses operator == to determine that Strings s3and s4refer to the same object. The fourth ifstructure (lines 45 50) uses operator ==to determine that Strings s1 and s3are not the same object. Technically, they could refer to the same object, but they are not guaranteed to refer to the same object unless the objects they refer to were returned by calls to intern on Strings with the same contents. In this case, s1 refers to the Stringit was assigned in method mainand s3refers to the Stringwith the same contents maintained by class String. The fifth ifstructure (lines 53 57) uses operator == to determine that Strings s2 and s4are not the same object, because the second interncall results in a reference to the same object returned by s1.intern(), not s2. Similarly, the sixth if structure (lines 60 64) uses operator == to determine that Strings s1 and s4 are not the same object, because the second interncall results in a reference to the same object returned by s1.intern(), not s1. 10.13 StringBufferClass The Stringclass provides many capabilities for processing Strings. However, once a Stringobject is created, its contents can never change. The next several sections discuss the features of class StringBufferfor creating and manipulating dynamic string information i.e., modifiable Strings. Every StringBufferis capable of storing a number of characters specified by its capacity. If the capacity of a StringBuffer is exceeded, the capacity is automatically expanded to accommodate the additional charac Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

My web server - 558 Strings and Characters Chapter 10 14 15

Tuesday, November 13th, 2007

558 Strings and Characters Chapter 10 14 15 s1 = new String( “hello” ); 16 s2 = new String( “hello” ); 17 18 // test strings to determine if they are same 19 // String object in memory 20 if ( s1 == s2 ) 21 output = “s1 and s2 are the same object in memory”; 22 else 23 output = “s1 and s2 are not the same object in memory”; 24 25 // test strings for equality of contents 26 if ( s1.equals( s2 ) ) 27 output += “ns1 and s2 are equal”; 28 else 29 output += “ns1 and s2 are not equal”; 30 31 // use String intern method to get a unique copy of 32 // “hello” referred to by both s3 and s4 33 s3 = s1.intern(); 34 s4 = s2.intern(); 35 36 // test strings to determine if they are same 37 // String object in memory 38 if ( s3 == s4 ) 39 output += “ns3 and s4 are the same object in memory”; 40 else 41 output += 42 “ns3 and s4 are not the same object in memory”; 43 44 // determine if s1 and s3 refer to same object 45 if ( s1 == s3 ) 46 output += 47 “ns1 and s3 are the same object in memory”; 48 else 49 output += 50 “ns1 and s3 are not the same object in memory”; 51 52 // determine if s2 and s4 refer to same object 53 if ( s2 == s4 ) 54 output += “ns2 and s4 are the same object in memory”; 55 else 56 output += 57 “ns2 and s4 are not the same object in memory”; 58 59 // determine if s1 and s4 refer to same object 60 if ( s1 == s4 ) 61 output += “ns1 and s4 are the same object in memory”; 62 else 63 output += 64 “ns1 and s4 are not the same object in memory”; 65 Fig. 10.11 Stringclass internmethod (part 2 of 3). Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

Florida web design - Chapter 10 Strings and Characters 557 copies a

Tuesday, November 13th, 2007

Chapter 10 Strings and Characters 557 copies a portion of the contents of the character array charArray into a new String object and returns the new String. The second argument specifies the starting index from which the characters are copied. The third argument specifies the number of characters to copy. There are seven other versions of method valueOf, which take arguments of type boolean, char, int, long, float, double and Object, respectively. These are demonstrated in lines 26 32 of the program. Note that the version of valueOfthat takes an Objectas an argument can do so because all Objects can be converted to Strings with method toString. 10.12 StringMethod intern Comparing large Stringobjects is a relatively slow operation. Stringmethod intern can improve String comparison performance. When String method intern is invoked on a Stringobject, it returns a reference to a Stringobject that is guaranteed to have the same contents as the original String. Class String maintains the resulting String during program execution. Subsequently, if the program invokes method intern on other String objects with contents identical to the original String, method intern returns a reference to the copy of the String maintained in memory by class String. If a program uses internon extremely large Strings, the program can compare those Strings faster by using the ==operator, which simply compares two references a fast operation. Using standard String methods such as equals and equalsIgnoreCase can be slower, because they compare corresponding characters in each String. For large Strings, this is a time-consuming, iterative operation. The program of Fig. 10.11 demonstrates the internmethod. The program declares five String references s1, s2, s3, s4 and output. Strings s1and s2are initialized with new Stringobjects that each contain a copy of “hello”. The first if structure (lines 20 23) uses operator == to determine that Strings s1and s2are the same object. References s1and s2refer to different objects, because they were initialized with new Stringobjects. The second ifstructure (lines 26 29) uses method equalsto determine that the contents of Strings s1 and s2 are equal. They were each initialized with copies of “hello”, so they have the same contents. 1 // Fig. 10.11: StringIntern.java 2 // This program demonstrates the intern method 3 // of the String class. 4 5 // Java extension packages 6 import javax.swing.*; 7 8 public class StringIntern { 9 10 // test String method intern 11 public static void main( String args[] ) 12 { 13 String s1, s2, s3, s4, output; Fig. 10.11 Stringclass internmethod (part 1 of 3). Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

556 Strings and (Michigan web site) Characters Chapter 10 7 public

Monday, November 12th, 2007

556 Strings and Characters Chapter 10 7 public class StringValueOf { 8 9 // test String valueOf methods 10 public static void main( String args[] ) 11 { 12 char charArray[] = { ‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’ }; 13 boolean b = true; 14 char c = ‘Z’; 15 int i = 7; 16 long l = 10000000; 17 float f = 2.5f; 18 double d = 33.333; 19 20 Object o = “hello”; // assign to an Object reference 21 String output; 22 23 output = “char array = ” + String.valueOf( charArray ) + 24 “npart of char array = ” + 25 String.valueOf( charArray, 3, 3 ) + 26 “nboolean = ” + String.valueOf( b ) + 27 “nchar = ” + String.valueOf( c ) + 28 “nint = ” + String.valueOf( i ) + 29 “nlong = ” + String.valueOf( l ) + 30 “nfloat = ” + String.valueOf( f ) + 31 “ndouble = ” + String.valueOf( d ) + 32 “nObject = ” + String.valueOf( o ); 33 34 JOptionPane.showMessageDialog( null, output, 35 “Demonstrating String Class valueOf Methods”, 36 JOptionPane.INFORMATION_MESSAGE ); 37 38 System.exit( 0 ); 39 } 40 41 } // end class StringValueOf Fig. 10.10 Stringclass valueOfmethods (part 2 of 2). The expression String.valueOf( charArray ) from line 23 copies the contents of the character array charArray into a new String object and returns the new String. The expression String.valueOf( charArray, 3, 3 ) from line 25 Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

Chapter 10 Strings and Characters 555 Fig. 10.9 (Web hosting reviews)

Sunday, November 11th, 2007

Chapter 10 Strings and Characters 555 Fig. 10.9 Miscellaneous Stringmethods (part 2 of 2). Line 30 uses Stringmethod trimto generate a new Stringobject that removes all white-space characters that appear at the beginning or end of the Stringto which the trim message is sent. The method returns a new Stringobject containing the Stringwithout leading or trailing white-space characters. The original Stringremains unchanged. Line 33 uses String method toString to return the String s1. Why is the toStringmethod provided for class String? All objects can be converted to Strings in Java by using method toString, which originates in class Object. If a class that inherits from Object(such as String) does not override method toString, the default version from class Objectis used. The default version in Objectcreates a Stringconsisting of the object s class name and the hash code for the object. The toStringmethod normally is used to express the contents of an object as text. Method toStringis provided in class Stringto ensure that the proper Stringvalue is returned. Line 36 creates a new character array containing a copy of the characters in String s1and assigns it to charArray. 10.11 Using StringMethod valueOf Class String provides a set of static class methods that take arguments of various types, convert those arguments to Strings and return them as String objects. Class StringValueOf(Fig. 10.10) demonstrates the Stringclass valueOf methods. 1 // Fig. 10.10: StringValueOf.java 2 // This program demonstrates the String class valueOf methods. 3 4 // Java extension packages 5 import javax.swing.*; 6 Fig. 10.10 Stringclass valueOfmethods (part 1 of 2). Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

Web design course - 554 Strings and Characters Chapter 10 1 //

Sunday, November 11th, 2007

554 Strings and Characters Chapter 10 1 // Fig. 10.9: StringMiscellaneous2.java 2 // This program demonstrates the String methods replace, 3 // toLowerCase, toUpperCase, trim, toString and toCharArray 4 5 // Java extension packages 6 import javax.swing.*; 7 8 public class StringMiscellaneous2 { 9 10 // test miscellaneous String methods 11 public static void main( String args[] ) 12 { 13 String s1 = new String( “hello” ), 14 s2 = new String( “GOOD BYE” ), 15 s3 = new String( ” spaces ” ); 16 17 String output = “s1 = ” + s1 + “ns2 = ” + s2 + 18 “ns3 = ” + s3; 19 20 // test method replace 21 output += “nnReplace ‘l’ with ‘L’ in s1: ” + 22 s1.replace( ‘l’, ‘L’ ); 23 24 // test toLowerCase and toUpperCase 25 output += 26 “nns1.toUpperCase() = ” + s1.toUpperCase() + 27 “ns2.toLowerCase() = ” + s2.toLowerCase(); 28 29 // test trim method 30 output += “nns3 after trim = “” + s3.trim() + “”"; 31 32 // test toString method 33 output += “nns1 = ” + s1.toString(); 34 35 // test toCharArray method 36 char charArray[] = s1.toCharArray(); 37 38 output += “nns1 as a character array = “; 39 40 for ( int count = 0; count < charArray.length; ++count ) 41 output += charArray[ count ]; 42 43 JOptionPane.showMessageDialog( null, output, 44 “Demonstrating Miscellaneous String Methods”, 45 JOptionPane.INFORMATION_MESSAGE ); 46 47 System.exit( 0 ); 48 } 49 50 } // end class StringMiscellaneous2 Fig. 10.9 Miscellaneous Stringmethods (part 1 of 2). Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

Michigan web site - Chapter 10 Strings and Characters 553 11 //

Saturday, November 10th, 2007

Chapter 10 Strings and Characters 553 11 // test String method concat 12 public static void main( String args[] ) 13 { 14 String s1 = new String( “Happy ” ), 15 s2 = new String( “Birthday” ); 16 17 String output = “s1 = ” + s1 + “ns2 = ” + s2; 18 19 output += “nnResult of s1.concat( s2 ) = ” + 20 s1.concat( s2 ); 21 22 output += “ns1 after concatenation = ” + s1; 23 24 JOptionPane.showMessageDialog( null, output, 25 “Demonstrating String Method concat”, 26 JOptionPane.INFORMATION_MESSAGE ); 27 28 System.exit( 0 ); 29 } 30 31 } // end class StringConcatenation Fig. 10.8 Stringmethod concat(part 2 of 2). 10.10 Miscellaneous StringMethods Class String provides several methods that return modified copies of Strings or that return a character array. These methods are demonstrated in the application of Fig. 10.9. Line 22 uses Stringmethod replaceto return a new Stringobject in which the method replaces every occurrence in Strings1of character ‘l’(el) with character ‘L’. Method replaceleaves the original Stringunchanged. If there are no occurrences of the first argument in the String, Method replacereturns the original String. Line 26 uses Stringmethod toUpperCaseto generate a new Stringobject with uppercase letters where corresponding lowercase letters reside in s1. The method returns a new Stringobject containing the converted Stringand leaves the original String unchanged. If there are no characters to convert to uppercase letters, method toUpper- Casereturns the original String. Line 27 uses Stringmethod toLowerCase to return a new String object with lowercase letters where corresponding uppercase letters reside in s1. The original String remains unchanged. If there are no characters to convert to lowercase letters, method toLowerCasereturns the original String. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01