Java Character Class and Methods

We have worked with characters (char) as primitive data types. Now, we are ready to examine the Character class which offers a number of useful methods for manipulating characters.

Character is located in the java.lang package.
The full class name is java.lang.Character.
Remember, NO importing is needed to utilize the java.lang package.

Sample ways to engage these new methods:
System.out.println(Character.isDigit(value)); //prints the result
char c = Character.toLowerCase('Q'); //c becomes 'q'

NOTE: All of these methods take a char as a parameter.

You cannot pass an entire string to character methods. If you want to test the elements of a string, you must pass the string one element at a time.
• If you pass an integer to these methods, the methods will act upon the corresponding ASCII character associated with your integer (i.e. int i = 65; and char j = 'A'; are the same since in ASICC the letter A is assigned to 65). Read about ASCII below the table.

Method
Purpose
Character.isLetter() determines whether a char value is a letter and returns "true" if the char is an upper or lower case letter.
Character.isDigit() determines whether a char value is a digit and returns "true" if the char is 0 through 9.
Character.isLetterOrDigit() determines whether a char value is a letter or a digit and returns "true" if that is the case.
Character.isascii() returns "true" if the integer argument is in the ASCII range 0-127. Treats 128-255 as non-ASCII
Character.isWhitespace() determines whether a char value is white space (such as a space, tab, or newline) and returns "true" if this is the case.
Character.isLowerCase() determines whether a char value is lowercase and returns "true" if this is the case.
Character.isUpperCase() determines whether a char value is uppercase and returns "true" if this is the case.
Character.toLowerCase() returns the lowercase form of a char value
c = Character.toLowerCase('Q'); //c becomes 'q'
Character.toUpperCase() returns the uppercase form of a char value
c = Character.toUpperCase('q'); //c becomes 'Q'
Character.toString() returns a one-character String to represent the char value
Character.getNumericValue() returns the int value associated with the character.

Java works with the American Standard Code for Information Interchange (ASCII) character set. The Character class includes character classification methods. A character is passed to the functions and the functions return values that can be stored or printed.

Basic ASCII Printable Characters:

Category
ASCII Characters
Uppercase letters 'A' through 'Z' (ASCII 65 to 90)
Lowercase letters 'a' through 'z' (ASCII 97 to 122)
Digits (0 through 9) '0' through '9' (ASCII 48 to 57)
Whitespace Space, tab, line feed (newline), and carriage return
Punctuation !"#$%&'()*+,-./ (ASCII 33 to 47)
:;<=>?@ (ASCII 58 to 64)
[\]^_ (ASCII 91 to 95)
{|}~ (ASCII 123 to 126)
Blank space the blank space character (ASCII 32)

There are 128 values in ASCII.
For example, the letter 'a' is the value 97 and 'b' is the value 98.
The letters "a" through "z" can be converted to 0-26 by subtracting 97.
The digit characters 0 to 9 can be converted to actual digits by subtracting 48.

divider
Return to Unit Menu |  JavaBitsNotebook.com | MathBits.com | Terms of Use  | JavaMathBits.com