Chapter 10 Strings and Characters 539 7 public (Kids web site)

Chapter 10 Strings and Characters 539 7 public class StringConstructors { 8 9 // test String constructors 10 public static void main( String args[] ) 11 { 12 char charArray[] = { ‘b’, ‘i’, ‘r’, ‘t’, ‘h’, ‘ ‘, 13 ‘d’, ‘a’, ‘y’ }; 14 byte byteArray[] = { ( byte ) ‘n’, ( byte ) ‘e’, 15 ( byte )’w', ( byte) ‘ ‘, ( byte ) ‘y’, 16 ( byte ) ‘e’, ( byte ) ‘a’, ( byte ) ‘r’ }; 17 18 StringBuffer buffer; 19 String s, s1, s2, s3, s4, s5, s6, s7, output; 20 21 s = new String( “hello” ); 22 buffer = new StringBuffer( “Welcome to Java Programming!” ); 23 24 // use String constructors 25 s1 = new String(); 26 s2 = new String( s ); 27 s3 = new String( charArray ); 28 s4 = new String( charArray, 6, 3 ); 29 s5 = new String( byteArray, 4, 4 ); 30 s6 = new String( byteArray ); 31 s7 = new String( buffer ); 32 33 // append Strings to output 34 output = “s1 = ” + s1 + “ns2 = ” + s2 + “ns3 = ” + s3 + 35 “ns4 = ” + s4 + “ns5 = ” + s5 + “ns6 = ” + s6 + 36 “ns7 = ” + s7; 37 38 JOptionPane.showMessageDialog( null, output, 39 “Demonstrating String Class Constructors”, 40 JOptionPane.INFORMATION_MESSAGE ); 41 42 System.exit( 0 ); 43 } 44 45 } // end class StringConstructors Fig. 10.1 Demonstrating the Stringclass constructors (part 2 of 2). Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

Leave a Reply