Java Character class
The Character class is a wrapper that is used to wrap a value of the primitive type char in an object. An object of class Character contains a single field whose type is char.
This class provides a large number of static methods for determining a character’s category (lowercase letter, digit, etc.) and for converting characters from uppercase to lowercase and vice versa.
This class is located into java.lang package and the declaration is like below.
Declaration
Here we are explaining methods of Character class with the help of examples.
1. isLetter(char ch)
This method is used to determine if the specified character is a letter or not. It returns a boolean value either true or false. If the character is a letter then it returns true else false. It takes a single char type argument. General syntax of the method is given below.
Syntax:
Example:
Lets take an example to check whether the specified character is a letter or not. We used isLetter()
method to ensure this. See the below example.
Â
2. isDigit(char ch)
The Character class provides a isDigit()
method to determine if the specified character is a digit or not. It returns true if the specified character is a digit, false otherwise.
It takes a single char type argument. General syntax of the method is given below.
Syntax:
Example:
In this example, we are checking of the specified character is a digit or not. It returns true if the specified character is a digit.
Â
3. isWhitespace(char ch)
This method is used to determine whether the specified character is a white space or not. A character is a Java whitespace character if it is a Unicode space character. It returns true if the character is a Java whitespace, false otherwise.
It takes a single char type argument. General syntax of the method is given below.
Syntax:
Example:
We can use isWhitespace()
method to check whether the specified character is a whitespace or not, as we did in the below example.
Â
4. isUpperCase(char ch)
The isUpperCase()
method is used to determine if the specified character is an uppercase character or not. It returns true if the specified character is upper case letter, false otherwise.
It takes a single char type argument. General syntax of the method is given below.
Syntax:
Example:
Lets take an example to check whether the specified character is uppercase letter or not.
Â
5. isLowerCase(char ch)
This method is used to check whether the specified character is lowercase letter or not. It returns true if the specified character is in lowercase, false otherwise. Syntax of the method is given below.
Syntax:
Example:
To check the character lower case, we can use isLowerCase() method that returns true.
Â
6. toUpperCase(char ch)
This method is used to convert the character argument to uppercase. It returns a character after converting to the uppercase. It takes a single argument of char type. Syntax of the method is given below.
Syntax:
Example:
In case if we want to convert a character into uppercase then we can use this method that returns a character after converting to the uppercase.
Â
7. toLowerCase(char ch)
This method is used to convert the character argument to lowercase letter. It returns a character after converting to the lowercase. It takes an single character type argument. Declaration of this method is given below.
Syntax:
Example:
we are creating an example that convert a character to its lowercase. It uses toLowerCase()
method for character case conversion.
Â
8. toString(char ch)
The toString()
method is used to get a String object representing the specified char. The result is a string of length 1 consisting solely of the specified char.
This method takes a single character as an argument and returns a string. Declaration of this method is given below.
Syntax:
Example:
Â
9. charCount(int codePoint)
This method is used to determine the number of char values needed to represent the specified character
It takes an int type argument as code point and returns 2, if the specified character is equal to or greater than 0x10000, 1 otherwise.
Syntax:
Example:
Lets take an example to use charCount() method that will count char for the code point.
Â
10. charValue()
The charValue()
method is used to get a primitive char type value from the char object. It returns a char value of this Character object. Declaration of this method is given below.
Syntax:
Example:
In this example, we are getting a primitive char from the Character object by using the charValue()
method.
Â