Home > Articles

This chapter is from the book

7.5 Class Character

Java provides eight type-wrapper classes that treat primitive-type values as objects—Boolean, Character, Double, Float, Byte, Short, Integer and Long. This section presents class Character—the type-wrapper class for primitive type char.

Class Character4 provides static methods for conveniently processing individual char values. These methods generally receive a char argument and either test it or manipulate it. For more information on class Character (and all the type-wrapper classes), refer to the java.lang package in the Java API documentation.

7.5.1 Character Testing and Case Conversion Methods

Figure 7.15 demonstrates

  • static Character methods that test characters to determine whether they’re a specific character type,

  • static Character methods that perform case conversions on characters.

Many of the Character testing methods shown here (and the techniques in Section 7.8) are useful for validating program inputs. Lines 8–10 input a character to process. Scanner’s next method (line 9) reads characters until it encounters a whitespace character, then returns a String containing the characters up to, but not including, the whitespace character, which is discarded.

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

Fig. 7.15 | Character static methods for testing characters and converting case.

Each Character method that begins with the word "is" returns true or false:

  • Line 13 uses the Character method isDefined to determine whether character c is defined in the Unicode character set.

  • Line 14 uses the Character method isDigit to determine whether character c is a defined Unicode digit.

  • Line 16 uses the Character method isJavaIdentifierStart to determine whether c is a character that can be the first character of a Java identifier—a letter, an underscore (_) or a dollar sign ($).

  • Line 18 uses the Character method isJavaIdentifierPart to determine whether character c is a character that can be used in an identifier in Java—that is, a digit, a letter, an underscore (_) or a dollar sign ($).

  • Line 19 uses the Character method isLetter to determine whether character c is a letter.

  • Line 21 uses Character method isLetterOrDigit to determine whether character c is a letter or a digit.

  • Line 23 uses the Character method isLowerCase to determine whether character c is a lowercase letter.

  • Line 25 uses the Character method isUpperCase to determine whether character c is an uppercase letter.

  • Line 27 uses the Character method toUpperCase to convert c to its uppercase equivalent. The method returns the converted character if the character has an uppercase equivalent; otherwise, the method returns its argument.

  • Line 29 uses the Character method toLowerCase to convert c to its lowercase equivalent. The method returns the converted character if the character has a lowercase equivalent; otherwise, the method returns its argument.

7.5.2 Character/Digit Conversions

Figure 7.16 demonstrates Character methods digit and forDigit, which convert characters to digits and vice versa in different number systems—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, refer to Appendix C.

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

Fig. 7.16 | Character class static conversion methods.

Line 27 uses the method forDigit to convert the integer digit into a character in the number system specified by the integer radix (the number’s base). For example, the decimal integer 13 in base 16 (the radix) has the character value 'd'. Lowercase and uppercase letters represent the same value in number systems. Line 33 uses the method digit to convert variable character 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.

7.5.3 Other Character Methods

Figure 7.17 demonstrates the Character class instance methodscharValue, toString and equals. Lines 5–6 instantiate two Character objects by assigning the character constants 'A' and 'a', respectively, to the Character variables. Java autoboxes these chars into Character objects. Line 9 uses the Character method charValue to get the char stored in c1 and uses method toString to get a String representation of c2. The condition in line 11 uses method equals to determine whether the chars in the objects c1 and c2 are equal. You’d generally work with Character objects and their instance methods only when storing chars in a collection, such as an ArrayList<Character>. Given Java’s ability to box chars into Character objects and unbox chars from Character objects, the Character instance methods are rarely used.

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

c1 and c2 are not equal

Fig. 7.17 | Character class instance methods.

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.