Class Character

Recall from Chapter 17 that Java provides eight type-wrapper classesBoolean, Character, Double, Float, Byte, Short, Integer and Longthat enable primitive-type values to be treated as objects. In this section, we present class Characterthe type-wrapper class for primitive type char.

Most Character methods are static methods designed to be convenience methods for processing individual char values. These methods take at least a character argument and perform either a test or a manipulation of the character. Class Character also contains a constructor that receives a char argument to initialize a Character object. Most of the methods of class Character are presented in the next three examples. For more information on class Character (and all the type-wrapper classes), see the java.lang package in the Java API documentation.

Figure 29.15 demonstrates some static methods that test characters to determine whether they are a specific character type and the static methods that perform case conversions on characters. You can enter any character and apply the methods to the character.

Figure 29.15. Character class static methods for testing characters and converting character case.

(This item is displayed on pages 1373 - 1374 in the print version)

1 // Fig. 29.15: StaticCharMethods.java 2 // Static Character testing methods and case conversion methods. 3 import java.util.Scanner; 4 5 public class StaticCharMethods 6 { 7 public static void main( String args[] ) 8 { 9 Scanner scanner = new Scanner( System.in ); // create scanner 10 System.out.println( "Enter a character and press Enter" ); 11 String input = scanner.next(); 12 char c = input.charAt( 0 ); // get input character 13 14 // display character info 15 System.out.printf( "is defined: %b ", Character.isDefined( c ) ); 16 System.out.printf( "is digit: %b ", Character.isDigit( c ) ); 17 System.out.printf( "is first character in a Java identifier: %b ", 18 Character.isJavaIdentifierStart( c ) ); 19 System.out.printf( "is part of a Java identifier: %b ", 20 Character.isJavaIdentifierPart( c ) ); 21 System.out.printf( "is letter: %b ", Character.isLetter( c ) ); 22 System.out.printf( 23 "is letter or digit: %b ", Character.isLetterOrDigit( c ) ); 24 System.out.printf( 25 "is lower case: %b ", Character.isLowerCase( c ) ); 26 System.out.printf( 27 "is upper case: %b ", Character.isUpperCase( c ) ); 28 System.out.printf( 29 "to upper case: %s ", Character.toUpperCase( c ) ); 30 System.out.printf( 31 "to lower case: %s ", Character.toLowerCase( c ) ); 32 } // end main 33 } // end class StaticCharMethods  

Enter a character and press Enter A is defined: true is digit: false is first character in a Java identifier: true is part of a Java identifier: true is letter: true is letter or digit: true is lower case: false is upper case: true to upper case: A to lower case: a  

 

Enter a character and press Enter 8 is defined: true is digit: true is first character in a Java identifier: false is part of a Java identifier: true is letter: false is letter or digit: true is lower case: false is upper case: false to upper case: 8 to lower case: 8  

 

Enter a character and press Enter $ is defined: true is digit: false is first character in a Java identifier: true is part of a Java identifier: true is letter: false is letter or digit: false is lower case: false is upper case: false to upper case: $ to lower case: $  

Line 15 uses Character method isDefined to determine whether character c is defined in the Unicode character set. If so, the method returns true, and otherwise, it returns false. Line 16 uses Character method isDigit to determine whether character c is a defined Unicode digit. If so, the method returns TRue, and otherwise, it returns false.

Line 18 uses Character method isJavaIdentifierStart to determine whether c is a character that can be the first character of an identifier in Javathat is, a letter, an underscore (_) or a dollar sign ($). If so, the method returns TRue, and otherwise, it returns false. Line 20 uses Character method isJavaIdentifierPart to determine whether character c is a character that can be used in an identifier in Javathat is, a digit, a letter, an underscore (_) or a dollar sign ($). If so, the method returns true, and otherwise, it returns false.

Line 21 uses Character method isLetter to determine whether character c is a letter. If so, the method returns true, and otherwise, it returns false. Line 23 uses Character method isLetterOrDigit to determine whether character c is a letter or a digit. If so, the method returns true, and otherwise, it returns false.

Line 25 uses Character method isLowerCase to determine whether character c is a lowercase letter. If so, the method returns true, and otherwise, it returns false. Line 27 uses Character method isUpperCase to determine whether character c is an uppercase letter. If so, the method returns true, and otherwise, it returns false.

Line 29 uses Character method toUpperCase to convert the character c to its uppercase equivalent. The method returns the converted character if the character has an uppercase equivalent, and otherwise, the method returns its original argument. Line 31 uses Character method toLowerCase to convert the character c to its lowercase equivalent. The method returns the converted character if the character has a lowercase equivalent, and otherwise, the method returns its original argument.

Figure 29.16 demonstrates static Character methods digit and forDigit, which convert characters to digits and digits to characters, respectively, in different number systems. Common number systems include decimal (base 10), octal (base 8), hexadecimal (base 16) and binary (base 2). The base of a number is also known as its radix. For more information on conversions between number systems, see Appendix E.

Figure 29.16. Character class static conversion methods.

(This item is displayed on pages 1375 - 1376 in the print version)

1 // Fig. 29.16: StaticCharMethods2.java 2 // Static Character conversion methods. 3 import java.util.Scanner; 4 5 public class StaticCharMethods2 6 { 7 // create StaticCharMethods2 object execute application 8 public static void main( String args[] ) 9 { 10 Scanner scanner = new Scanner( System.in ); 11 12 // get radix 13 System.out.println( "Please enter a radix:" ); 14 int radix = scanner.nextInt(); 15 16 // get user choice 17 System.out.printf( "Please choose one: 1 -- %s 2 -- %s ", 18 "Convert digit to character", "Convert character to digit"); 19 int choice = scanner.nextInt(); 20 21 // process request 22 switch ( choice ) 23 { 24 case 1 : // convert digit to character 25 System.out.println( "Enter a digit:" ); 26 int digit = scanner.nextInt(); 27 System.out.printf( "Convert digit to character: %s ", 28 Character.forDigit( digit, radix )); 29 break; 30 31 case 2 : // convert character to digit 32 System.out.println( "Enter a character:" ); 33 char character = scanner.next().charAt( 0 ); 34 System.out.printf( "Convert character to digit: %s ", 35 Character.digit( character, radix )); 36 break; 37 } // end switch 38 } // end main 39 } // end class StaticCharMethods2  

Please enter a radix: 16 Please choose one: 1 -- Convert digit to character 2 -- Convert character to digit 2 Enter a character: A Convert character to digit: 10  

 

Please enter a radix: 16 Please choose one: 1 -- Convert digit to character 2 -- Convert character to digit 1 Enter a digit: 13 Convert digit to character: d  

Line 28 uses method forDigit to convert the integer digit into a character in the number system specified by the integer radix (the base of the number). For example, the decimal integer 13 in base 16 (the radix) has the character value 'd'. Note that lowercase letters represent the same value as uppercase letters in number systems. Line 35 uses method digit to convert the character c into an integer in the number system specified by the integer radix (the base of the number). For example, the character 'A' is the base 16 (the radix) representation of the base 10 value 10. The radix must be between 2 and 36, inclusive.

The application in Fig. 29.17 demonstrates the constructor and several non-static methods of class CharactercharValue, toString and equals. Lines 89 instantiate two Character objects and pass character literals to the constructor to initialize those objects. Line 12 uses Character method charValue to return the char value stored in Character object c1. Line 12 returns a string representation of Character object c2 using method toString. The condition in the if... else statement at lines 1417 uses method equals to determine whether the object c1 has the same contents as the object c2 (i.e., the characters inside each object are equal).

Figure 29.17. Character class non-static methods.

(This item is displayed on page 1377 in the print version)

1 // Fig. 29.17: OtherCharMethods.java 2 // Non-static Character methods. 3 4 public class OtherCharMethods 5 { 6 public static void main( String args[] ) 7 { 8 Character c1 = 'A'; 9 Character c2 = 'a'; 10 11 System.out.printf( 12 "c1 = %s c2 = %s ", c1.charValue(), c2.toString() ); 13 14 if ( c1.equals( c2 ) ) 15 System.out.println( "c1 and c2 are equal " ); 16 else 17 System.out.println( "c1 and c2 are not equal " ); 18 } // end main 19 } // end class OtherCharMethods  

c1 = A c2 = a c1 and c2 are not equal  

Категории