Character Helper Methods
isDigit, isLetter, toUpperCase.
The Character Class
Java's Character class provides handy static helper methods for working with chars.
You call them on the class itself, passing a char as the argument, like Character.isDigit(c). They return useful answers without you doing math by hand.
char c = '4';
boolean digit = Character.isDigit(c);Checking for Digits
Character.isDigit(c) returns true when the char is a number from '0' to '9'.
This is cleaner and safer than comparing codes yourself, and it also understands digits from other languages.
public class Main {
public static void main(String[] args) {
System.out.println(Character.isDigit('8'));
System.out.println(Character.isDigit('x'));
}
}All lessons in this course
- The char Primitive
- Character Helper Methods
- Looping Over Characters
- Counting and Filtering Chars