Chapter 10 Strings and Characters 575 7 8 (Web site hosting)

Chapter 10 Strings and Characters 575 7 8 public class OtherCharMethods { 9 10 // test non-static Character methods 11 public static void main( String args[] ) 12 { 13 Character c1, c2; 14 15 c1 = new Character( ‘A’ ); 16 c2 = new Character( ‘a’ ); 17 18 String output = “c1 = ” + c1.charValue() + 19 “nc2 = ” + c2.toString() + 20 “nnhash code for c1 = ” + c1.hashCode() + 21 “nhash code for c2 = ” + c2.hashCode(); 22 23 if ( c1.equals( c2 ) ) 24 output += “nnc1 and c2 are equal”; 25 else 26 output += “nnc1 and c2 are not equal”; 27 28 JOptionPane.showMessageDialog( null, output, 29 “Demonstrating Non-Static Character Methods”, 30 JOptionPane.INFORMATION_MESSAGE ); 31 32 System.exit( 0 ); 33 } 34 35 } // end class OtherCharMethods Fig. 10.19 Non-staticmethods of class Character(part 2 of 2). Lines 15 16 instantiate two Characterobjects and pass character literals to the constructor to initialize those objects. Line 18 uses Character method charValue to return the char value stored in Characterobject c1. Line 19 returns a Stringrepresentation of Characterobject c2using method toString. Lines 20 21perform hashCodecalculations on the Characterobjects c1and c2, respectively. Remember that hash code values are used to store objects in hash tables for fast lookup capabilities (see Chapter 20). Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

Leave a Reply