public class StringBufferCapLen
1: // Fig. 29.11: StringBufferCapLen.java
2: // StringBuffer length, setLength, capacity and ensureCapacity methods.
3:
4: public class StringBufferCapLen
5: {
6: public static void main( String args[] )
7: {
8: StringBuffer buffer = new StringBuffer( "Hello, how are you?" );
9:
10: System.out.printf( "buffer = %s\nlength = %d\ncapacity = %d\n\n",
11: buffer.toString(), buffer.length(), buffer.capacity() );
12:
13: buffer.ensureCapacity( 75 );
14: System.out.printf( "New capacity = %d\n\n", buffer.capacity() );
15:
16: buffer.setLength( 10 );
17: System.out.printf( "New length = %d\nbuf = %s\n",
18: buffer.length(), buffer.toString() );
19: } // end main
20: } // end class StringBufferCapLen
21:
22:
23: /**************************************************************************
24: * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and *
25: * Pearson Education, Inc. All Rights Reserved. *
26: * *
27: * DISCLAIMER: The authors and publisher of this book have used their *
28: * best efforts in preparing the book. These efforts include the *
29: * development, research, and testing of the theories and programs *
30: * to determine their effectiveness. The authors and publisher make *
31: * no warranty of any kind, expressed or implied, with regard to these *
32: * programs or to the documentation contained in these books. The authors *
33: * and publisher shall not be liable in any event for incidental or *
34: * consequential damages in connection with, or arising out of, the *
35: * furnishing, performance, or use of these programs. *
36: *************************************************************************/