The Character wrapper class has many useful methods. We will look at some of them.
boolean isDigit(char ch)
The method isDigit() accepts a char and checks if it is a digit. It will return true only if ch is a digit i.e. ch should be one of the following – ‘0’, ‘1’, ‘2’, ‘3’, ‘4’, ‘5’, ‘6’, ‘7’, ‘8’, ‘9’
Given below is an example.
3 | public static void main(String[] a) { |
4 | boolean b1 = Character.isDigit( '5' ); |
5 | boolean b2 = Character.isDigit( '-' ); |
6 | System.out.println(b1); |
7 | System.out.println(b2); |
Output
boolean isLetter(char ch)
This method returns true if ch is a letter. It can be either an uppercase letter – ‘A’, ‘B’, …. ‘Z’ or a lowercase letter.
Given below is an example program.
3 | public static void main(String[] a) { |
4 | boolean b1 = Character.isLetter( 's' ); |
5 | boolean b2 = Character.isLetter( 'R' ); |
6 | boolean b3 = Character.isLetter( '&' ); |
7 | System.out.println(b1); |
8 | System.out.println(b2); |
9 | System.out.println(b3); |
Output
boolean isLetterOrDigit(char ch)
Returns true if ch is either a letter or a digit i.e. ch can be ‘A’, ‘B’, … ‘Z’, ‘a’, ‘b’, … ‘z’, ‘0’, ‘1’, … ‘9’
An example program is given below.
3 | public static void main(String[] a) { |
4 | boolean b1 = Character.isLetterOrDigit( 's' ); |
5 | boolean b2 = Character.isLetterOrDigit( '9' ); |
6 | boolean b3 = Character.isLetterOrDigit( '&' ); |
7 | System.out.println(b1); |
8 | System.out.println(b2); |
9 | System.out.println(b3); |
Output
boolean isLowerCase(char ch)
Returns true if ch is a lower case letter i.e. ch is one of ‘a’, ‘b’, … ‘z’. If ch is an uppercase letter or not a letter, then it returns false.
Example program is given below.
3 | public static void main(String[] a) { |
4 | boolean b1 = Character.isLowerCase( 's' ); |
5 | boolean b2 = Character.isLowerCase( 'R' ); |
6 | boolean b3 = Character.isLowerCase( '&' ); |
7 | System.out.println(b1); |
8 | System.out.println(b2); |
9 | System.out.println(b3); |
Output
boolean isUpperCase(char ch)
This method is similar to the previous method except that it returns true if ch is an uppercase letter.
3 | public static void main(String[] a) { |
4 | boolean b1 = Character.isUpperCase( 's' ); |
5 | boolean b2 = Character.isUpperCase( 'R' ); |
6 | boolean b3 = Character.isUpperCase( '&' ); |
7 | System.out.println(b1); |
8 | System.out.println(b2); |
9 | System.out.println(b3); |
Output
boolean isWhiteSpace(char ch)
A whitespace can be a space, tab or a new line. (There are few other characters which qualify as whitespace characters but we will not look at them here). If ch is a whitespace character, then this method returns true. Else, it returns false.
3 | public static void main(String[] a) { |
4 | boolean b1 = Character.isWhitespace( ' ' ); |
5 | boolean b2 = Character.isWhitespace( '\t' ); |
6 | boolean b3 = Character.isWhitespace( '\n' ); |
7 | boolean b4 = Character.isWhitespace( '3' ); |
8 | System.out.println(b1); |
9 | System.out.println(b2); |
10 | System.out.println(b3); |
11 | System.out.println(b4); |
Output
char toLowerCase(char ch)
This method takes a char and returns its lowercase equivalent. For example, if ch is ‘A’, then this method returns ‘a’. If ch is already in lowercase, then it returns the same lowercase letter without making any modifications. If ch is not a letter, it returns the same character without making any changes. No error will occur.
3 | public static void main(String[] a) { |
4 | char c1 = Character.toLowerCase( 's' ); |
5 | char c2 = Character.toLowerCase( 'R' ); |
6 | char c3 = Character.toLowerCase( '&' ); |
7 | System.out.println(c1); |
8 | System.out.println(c2); |
9 | System.out.println(c3); |
Output
char toUpperCase(char ch)
This is similar to the above method except that it converts ch to uppercase.
3 | public static void main(String[] a) { |
4 | char c1 = Character.toUpperCase( 's' ); |
5 | char c2 = Character.toUpperCase( 'R' ); |
6 | char c3 = Character.toUpperCase( '&' ); |
7 | System.out.println(c1); |
8 | System.out.println(c2); |
9 | System.out.println(c3); |
Output