In this unit, we will learn some of the most commonly used character methods that are useful for performing operations on characters.
Similar to the String class, which provides methods to perform operations on strings, the Character class provides methods to perform operations on characters (char values).
Here are some commonly used character methods:
isLetter()
isDigit()
isWhitespace()
isUpperCase()
isLowerCase()
toUpperCase()
toLowerCase()
toString()
The isLetter() method checks whether the given character is an alphabet and returns a boolean value true if it is, otherwise false
Character.isLetter(value); |
Example
class Main { public static void main(String[] args) { char ch1 = 'a'; char ch2 = '4'; System.out.println(Character.isLetter(ch1)); // isLetter method called System.out.println(Character.isLetter(ch2)); // isLetter method called } } |
true
false
The isDigit() method checks whether the given character is a number/digit and returns a boolean value true if it is, otherwise false
Character.isDigit(value); |
Example
class Main { public static void main(String[] args) { char ch1 = 'a'; char ch2 = '4'; System.out.println(Character.isDigit(ch1)); // isDigit method called System.out.println(Character.isDigit(ch2)); // isDigit method called } } |
false
true
The isWhitespace() method checks whether the given character is a whitespace and returns a boolean value true if it is, otherwise false
Syntax
Character.isWhitespace(value); |
Example
class Main { public static void main(String[] args) { char ch1 = ' '; char ch2 = 'a'; System.out.println(Character.isWhitespace(ch1)); // isWhitespace method called System.out.println(Character.isWhitespace(ch2)); // isWhitespace method called } } |
true
false
The isUpperCase() method checks whether the given character is an uppercase letter and returns a boolean value true if it is, otherwise false
Character.isUpperCase(value); |
Example
class Main { public static void main(String[] args) { char ch1 = 'A'; char ch2 = 'a'; System.out.println(Character.isUpperCase(ch1)); // isUpperCase method called System.out.println(Character.isUpperCase(ch2)); // isUpperCase method called } } |
true
false
The isLowerCase() method checks whether the given character is a lowercase letter and returns a boolean value true if it is, otherwise false
Syntax
Character.isLowerCase(value); |
Example
class Main { public static void main(String[] args) { char ch1 = 'a'; char ch2 = 'A'; System.out.println(Character.isLowerCase(ch1)); // isLowerCase method called System.out.println(Character.isLowerCase(ch2)); // isLowerCase method called } } |
true
false
The toUpperCase() method converts the given lowercase letter to uppercase and returns the new character.
All characters other than lowercase remain unchanged.
Character.toUpperCase(value); |
Example
class Main { public static void main(String[] args) { char ch1 = 'a'; char ch2 = 'A'; char ch3 = '*'; System.out.println(Character.toUpperCase(ch1)); // toUpperCase method called System.out.println(Character.toUpperCase(ch2)); // toUpperCase method called System.out.println(Character.toUpperCase(ch3)); // toUpperCase method called } } |
A
A
*
As we can see, in the above output, ch2 and ch3 remain unchanged.
The toLowerCase() method converts the uppercase letter to lowercase and returns the new character.
All characters other than uppercase remain unchanged.
Character.toLowerCase(value); |
Example
class Main { public static void main(String[] args) { char ch1 = 'A'; char ch2 = 'a'; char ch3 = '*'; System.out.println(Character.toLowerCase(ch1)); // toLowerCase method called System.out.println(Character.toLowerCase(ch2)); // toLowerCase method called System.out.println(Character.toLowerCase(ch3)); // toLowerCase method called } } |
a
a
*
As we can see, in the above output, ch2 and ch3 remain unchanged.
The toString() method converts the character into the respective string. The method returns a String object representing the specified character value that is, a one-character string.
Character.toString(value); |
Let's check the working of the toString() with an example by getting its data type.
Example
class Main { public static void main(String[] args) { char ch1 = 'A'; System.out.println(Character.toString(ch1)); System.out.println((Character.toString(ch1)).getClass().getSimpleName()); } } |
A
String
In the above code, we can see that the character method returns a String value.
Passing string instead of a character
The character methods only accept char data type as input.
class Main { public static void main(String[] args) { String str = "*";
// String passed to character method System.out.println(Character.toUpperCase(str)); } } |
file.java:6: error: no suitable method found for toUpperCase(String) System.out.println(Character.toUpperCase(str)); ^ method Character.toUpperCase(char) is not applicable (argument mismatch; String cannot be converted to char) method Character.toUpperCase(int) is not applicable (argument mismatch; String cannot be converted to int) 1 error |
Here are the character methods we have learned
isLetter(): Checks whether the character is a letter.
isDigit(): Checks whether the character is a number.
isWhitespace(): Checks whether the character is a whitespace.
isUpperCase(): Checks whether the character is an uppercase letter.
isLowerCase(): Checks whether the character is a lowercase letter.
toUpperCase(): Converts the given character into uppercase letter.
toLowerCase(): Converts the given character into lowercase letter.
toString(): Converts the given character into a single character string representing the character