In Java a char
is a value, much like an int
. Often in programs we need to test for ranges (classes) of characters—as for digits, letters, spaces.
Instead of acting upon chars directly, we can use built-in static
methods on Character. The Character class
includes this logic—we just access it.
IsDigit
This method returns true if the char
passed to it is "0" through "9." It also supports Unicode so is more complex (and likely slower) than an if
-statement and range check.
public class Program { public static void main(String[] args) { char value1 = '9'; char value2 = 'Y'; char value3 = ' '; // See if these characters are digits. System.out.println(Character.isDigit(value1)); System.out.println(Character.isDigit(value2)); System.out.println(Character.isDigit(value3)); } }true false false
IsLetter
, for
-loopWith Character.isLetter
we detect lowercase and uppercase letters. Digits, spaces and punctuation are not considered letters. IsLetter
returns true or false.
charAt
method in this program to access each char
in the String
. We then test with isLetter
.public class Program { public static void main(String[] args) { String value = "abc123"; // Loop through characters in this String. for (int i = 0; i < value.length(); i++) { char c = value.charAt(i); // See if the character is a letter or not. if (Character.isLetter(c)) { System.out.println(c + " = LETTER"); } else { System.out.println(c); } } } }a = LETTER b = LETTER c = LETTER 1 2 3
IsLetterOrDigit
In my experience, this method is often useful. Most tokens are composed of letters and digits. So with isLetterOrDigit
we can easily skip past punctuation and spaces.
public class Program { public static void main(String[] args) { char value1 = 'U'; char value2 = '9'; char value3 = 'w'; char value4 = '*'; System.out.println(Character.isLetterOrDigit(value1)); System.out.println(Character.isLetterOrDigit(value2)); System.out.println(Character.isLetterOrDigit(value3)); System.out.println(Character.isLetterOrDigit(value4)); // [False] } }true true true false
IsLowerCase
, isUpperCase
These return true or false if the character is lowercase (or uppercase). This can be implemented (for ASCII) with a simple if
-check, but that is harder to read.
if
-check may be better. This is something that needs benchmarking.public class Program { public static void main(String[] args) { char[] values = { 'C', 'a', 't', '5' }; // See if the chars are lower or uppercase. for (char value : values) { System.out.println(value); System.out.println(Character.isLowerCase(value)); System.out.println(Character.isUpperCase(value)); } } }C false true a true false t true false 5 false false
IsWhitespace
This method is a new version of Character.isSpace
. It returns true if the character is whitespace (like a space, tab, or newline). It returns false on other chars.
Character.isWhitespace
is better than a complex if
-test: it is simpler and easier to read.public class Program { public static void main(String[] args) { // Test these characters for whitespace. System.out.println(Character.isWhitespace(' ')); System.out.println(Character.isWhitespace('\n')); System.out.println(Character.isWhitespace('Q')); } }true true false
More complex logic is possible. This program applies toLowerCase
and toUpperCase
to invert the casing of letters in a String
.
char
is uppercase, it is changed to lowercase, and the opposite. It uses isUpperCase
and isLowerCase
to test chars.invertCase
method gets a mutable char
array with toCharArray
. It tests and changes chars. It returns a new String
.public class Program { static String invertCase(String value) { // Convert to a char array. char[] array = value.toCharArray(); for (int i = 0; i < array.length; i++) { // Change uppercase letters to lowercase ones. // ... And change lower to upper. if (Character.isUpperCase(array[i])) { array[i] = Character.toLowerCase(array[i]); } else if (Character.isLowerCase(array[i])) { array[i] = Character.toUpperCase(array[i]); } } return new String(array); } public static void main(String[] args) { // Use invertCase method. System.out.println(invertCase("Dog")); System.out.println(invertCase(invertCase("Cat"))); } }dOG Cat
With the Character class
, we can generate a table of all 128 ASCII values. Programs can manipulate ASCII values to improve performance.
For optimization, we can store character transformations in a table. This reduces any character modification to a simple array element access.
One algorithm that uses isLetter
and toLowerCase
is a palindrome-detecting algorithm. It skips over whitespace and non-letter chars to detect palindromes.
Character-based algorithms are often faster than String
-based ones. Fewer allocations are needed: a char
is simpler than an entire String
. With the Character class
, we test char
types.