The Character class in Java, available in the java.lang package is a wrapper class used to represent a single char value as an object. It provides several useful static methods for character manipulation and supports automatic conversion between primitive and object types.
- Wrapper for char: A Character object wraps a single primitive char value and allows it to be used in object-oriented contexts.
- Object creation: Earlier, objects were created using new Character(char), but this constructor is deprecated; use Character.valueOf(char) instead.
- Utility methods: The class provides static methods like isDigit(), isLetter(), toUpperCase(), and toLowerCase() for character operations.
- Autoboxing and unboxing: Java automatically converts between char and Character when required, simplifying code and improving readability.
Syntax
Character.methodName(parameter)
- Parameter: char
- Return type: boolean, char, or int depending on the method
Note: Character class is immutable like String class i.e once it's object is created, it cannot be changed.
Commonly Used Methods in Character Class
1. boolean isLetter(char ch): This method is used to determine whether the specified char value(ch) is a letter or not. The method will return true if it is letter([A-Z],[a-z]), otherwise return false. In place of character, we can also pass ASCII value as an argument as char to int is implicitly typecasted in java.
Syntax:
boolean isLetter(char ch)
- Parameters: ch - a primitive character
- Return Type: It returns true if ch is an alphabet, otherwise, return false
public class Test {
public static void main(String[] args) {
System.out.println(Character.isLetter('A'));
System.out.println(Character.isLetter('0'));
}
}
Output
true false
2. boolean isDigit(char ch): This method is used to determine whether the specified char value(ch) is a digit or not. Here also we can pass ASCII value as an argument.
Syntax:
boolean isDigit(char ch)
- Parameter: ch - a primitive character
- Return Type: It returns true if ch is a digit, otherwise, return false
public class Test {
public static void main(String[] args) {
System.out.println(Character.isDigit('A'));
System.out.println(Character.isDigit('0'));
}
}
Output
false true
3. boolean isWhitespace(char ch): It determines whether the specified char value(ch) is white space. Whitespace includes space, tab, or newline.
Syntax:
boolean isWhitespace(char ch)
- Parameter: ch - a primitive character
- Return Type: It returns true if ch is whitespace, otherwise, returns false.
public class Test {
public static void main(String[] args) {
System.out.println(Character.isWhitespace('A'));
System.out.println(Character.isWhitespace(' '));
System.out.println(Character.isWhitespace('\n'));
System.out.println(Character.isWhitespace('\t'));
System.out.println(Character.isWhitespace(9));
System.out.println(Character.isWhitespace('9'));
}
}
Output
false true true true true false
4. boolean isUpperCase(char ch): It determines whether the specified char value(ch) is uppercase or not.
Syntax:
boolean isUpperCase(char ch)
- Parameter: ch - a primitive character
- Return Type: It returns true if ch is upper case, otherwise, returns false.
public class Test {
public static void main(String[] args) {
System.out.println(Character.isUpperCase('A'));
System.out.println(Character.isUpperCase('a'));
System.out.println(Character.isUpperCase(65));
}
}
Output
true false true
5. boolean isLowerCase(char ch): It determines whether the specified char value(ch) is lowercase or not.
Syntax:
boolean isLowerCase(char ch)
- Parameter: ch - a primitive character
- Return Type: It returns true if ch is lower case, otherwise, returns false.
public class Test {
public static void main(String[] args) {
System.out.println(Character.isLowerCase('A'));
System.out.println(Character.isLowerCase('a'));
System.out.println(Character.isLowerCase(97));
}
}
Output
false true true
6. char toUpperCase(char ch): It returns the uppercase of the specified char value(ch). If an ASCII value is passed, then the ASCII value of its uppercase will be returned.
Syntax:
char toUpperCase(char ch)
- Parameters: ch - a primitive character
- Return Type: It returns the uppercase form of the specified char value.
public class Test {
public static void main(String[] args) {
System.out.println(Character.toUpperCase('a'));
System.out.println(Character.toUpperCase(97));
System.out.println(Character.toUpperCase(48));
}
}
Output
A 65 48
7. char toLowerCase(char ch): It returns the lowercase of the specified char value(ch).
Syntax:
char toLowerCase(char ch)
- Parameter: ch - a primitive character
- Return Type: It returns the lowercase form of the specified char value.
public class Test {
public static void main(String[] args) {
System.out.println(Character.toLowerCase('A'));
System.out.println(Character.toLowerCase(65));
System.out.println(Character.toLowerCase(48));
}
}
Output
a 97 48
8. toString(char ch): It returns a String class object representing the specified character value(ch) i.e a one-character string. Here we cannot pass ASCII value.
Syntax:
String toString(char ch)
- Parameter: ch - a primitive character
- Return Type: It returns a String object.
public class Test {
public static void main(String[] args) {
System.out.println(Character.toString('x'));
System.out.println(Character.toString('Y'));
}
}
Output
x Y
Methods of Character Class in Java
| Method | Description |
|---|---|
| static int charCount?(int codePoint) | This method determines the number of char values needed to represent the specified character (Unicode code point). |
| char charValue?() | This method returns the value of this Character object. |
| static int codePointAt?(char[] a, int index) | This method returns the code point at the given index of the char array. |
| static int codePointAt?(char[] a, int index, int limit) | This method returns the code point at the given index of the char array, where only array elements with an index less than the limit can be used. |
| static int codePointAt?(CharSequence seq, int index) | This method returns the code point at the given index of the CharSequence. |
| static int codePointBefore?(char[] a, int index) | This method returns the code point preceding the given index of the char array. |
| static int codePointBefore?(char[] a, int index, int start) | This method returns the code point preceding the given index of the char array, where only array elements with index greater than or equal to start can be used. |
| static int codePointBefore?(CharSequence seq, int index) | This method returns the code point preceding the given index of the CharSequence. |
| static int codePointCount?(char[] a, int offset, int count) | This method returns the number of Unicode code points in a subarray of the char array argument. |
| static int codePointCount?(CharSequence seq, int beginIndex, int endIndex) | This method returns the number of Unicode code points in the text range of the specified char sequence. |
| static int codePointOf?(String name) | This method returns the code point value of the Unicode character specified by the given Unicode character name. |
| static int compare?(char x, char y) | This method compares two char values numerically. |
| int compareTo?(Character anotherCharacter) | This method compares two Character objects numerically. |
| static int digit?(char ch, int radix) | This method returns the numeric value of the character ch in the specified radix. |
| static int digit?(int codePoint, int radix) | This method returns the numeric value of the specified character (Unicode code point) in the specified radix. |
| boolean equals?(Object obj) | This method compares this object against the specified object. |
| static char forDigit?(int digit, int radix) | This method determines the character representation for a specific digit in the specified radix. |
| static byte getDirectionality?(char ch) | This method returns the Unicode directionality property for the given character. |
| static byte getDirectionality?(int codePoint) | This method returns the Unicode directionality property for the given character (Unicode code point). |
| static String getName?(int codePoint) | This method returns the Unicode name of the specified character codePoint, or null if the code point is unassigned. |
| static int getNumericValue?(char ch) | This method returns the int value that the specified Unicode character represents. |
| static int getNumericValue?(int codePoint) | This method returns the int value that the specified character (Unicode code point) represents. |
| static int getType?(char ch) | This method returns a value indicating a character's general category. |
| static int getType?(int codePoint) | This method returns a value indicating a character's general category. |
| int hashCode?() | This method returns a hash code for this Character; equal to the result of invoking charValue(). |
| static int hashCode?(char value) | This method returns a hash code for a char value; compatible with Character.hashCode(). |
| static char highSurrogate?(int codePoint) | This method returns the leading surrogate (a high surrogate code unit) of the surrogate pair representing the specified supplementary character (Unicode code point) in the UTF-16 encoding. |
| static boolean isAlphabetic?(int codePoint) | This method determines if the specified character (Unicode code point) is an alphabet. |
| static boolean isBmpCodePoint?(int codePoint) | This method determines whether the specified character (Unicode code point) is in the Basic Multilingual Plane (BMP). |
| static boolean isDefined?(char ch) | This method determines if a character is defined in Unicode. |
| static boolean isDefined?(int codePoint) | This method determines if a character (Unicode code point) is defined in Unicode. |
| static boolean isHighSurrogate?(char ch) | This method determines if the given char value is a Unicode high-surrogate code unit (also known as a leading-surrogate code unit). |
| static boolean isIdentifierIgnorable?(char ch) | This method determines if the specified character should be regarded as an ignorable character in a Java identifier or a Unicode identifier. |
| static boolean isIdentifierIgnorable?(int codePoint) | This method determines if the specified character (Unicode code point) should be regarded as an ignorable character in a Java identifier or a Unicode identifier. |
| static boolean isIdeographic?(int codePoint) | This method determines if the specified character (Unicode code point) is a CJKV (Chinese, Japanese, Korean, and Vietnamese) ideograph, as defined by the Unicode Standard. |
| static boolean isISOControl?(char ch) | This method determines if the specified character is an ISO control character. |
| static boolean isISOControl?(int codePoint) | This method determines if the referenced character (Unicode code point) is an ISO control character. |
| static boolean isJavaIdentifierPart?(char ch) | This method determines if the specified character may be part of a Java identifier as other than the first character. |
| static boolean isJavaIdentifierPart?(int codePoint) | This method determines if the character (Unicode code point) may be part of a Java identifier as other than the first character. |
| static boolean isJavaIdentifierStart?(char ch) | This method determines if the specified character is permissible as the first character in a Java identifier. |
| static boolean isJavaIdentifierStart?(int codePoint) | This method determines if the character (Unicode code point) is permissible as the first character in a Java identifier. |
| static boolean isLowSurrogate?(char ch) | This method determines if the given char value is a Unicode low-surrogate code unit (also known as trailing-surrogate code unit). |
| static boolean isLetterOrDigit?(char ch) | This method determines if the specified character is a letter or digit. |
| static boolean isLetterOrDigit?(int codePoint) | This method determines if the specified character (Unicode code point) is a letter or digit. |
| static boolean isMirrored?(char ch) | This method determines whether the character is mirrored according to the Unicode specification. |
| static boolean isMirrored?(int codePoint) | This method determines whether the specified character (Unicode code point) is mirrored according to the Unicode specification. |
| static boolean isSpaceChar?(char ch) | This method determines if the specified character is a Unicode space character. |
| static boolean isSpaceChar?(int codePoint) | This method determines if the specified character (Unicode code point) is a Unicode space character. |
| static boolean isSupplementaryCodePoint?(int codePoint) | This method determines whether the specified character (Unicode code point) is in the supplementary character range. |
| static boolean isSurrogate?(char ch) | This method determines if the given char value is a Unicode surrogate code unit. |
| static boolean isSurrogatePair?(char high, char low) | This method determines whether the specified pair of char values is a valid Unicode surrogate pair. |
| static boolean isTitleCase?(char ch) | This method determines if the specified character is a titlecase character. |
| static boolean isTitleCase?(int codePoint) | This method determines if the specified character (Unicode code point) is a titlecase character. |
| static boolean isUnicodeIdentifierPart?(char ch) | This method determines if the specified character may be part of a Unicode identifier as other than the first character. |
| static boolean isUnicodeIdentifierPart?(int codePoint) | This method determines if the specified character (Unicode code point) may be part of a Unicode identifier as other than the first character. |
| static boolean isUnicodeIdentifierStart?(char ch) | This method determines if the specified character is permissible as the first character in a Unicode identifier. |
| static boolean isUnicodeIdentifierStart?(int codePoint) | This method determines if the specified character (Unicode code point) is permissible as the first character in a Unicode identifier. |
| static boolean isValidCodePoint?(int codePoint) | This method determines whether the specified code point is a valid Unicode code point value. |
| static char lowSurrogate?(int codePoint) | This method returns the trailing surrogate (a low surrogate code unit) of the surrogate pair representing the specified supplementary character (Unicode code point) in the UTF-16 encoding. |
| static int offsetByCodePoints?(char[] a, int start, int count, int index, int codePointOffset) | This method returns the index within the given char subarray that is offset from the given index by codePointOffset code points. |
| static int offsetByCodePoints?(CharSequence seq, int index, int codePointOffset) | This method returns the index within the given char sequence that is offset from the given index by codePointOffset code points. |
| static char reverseBytes?(char ch) | This method returns the value obtained by reversing the order of the bytes in the specified char value. |
| static char[] toChars?(int codePoint) | This method converts the specified character (Unicode code point) to its UTF-16 representation stored in a char array. |
| static int toChars?(int codePoint, char[] dst, int dstIndex) | This method converts the specified character (Unicode code point) to its UTF-16 representation. |
| static int toCodePoint?(char high, char low) | This method converts the specified surrogate pair to its supplementary code point value. |
| static char toTitleCase?(char ch) | This method converts the character argument to titlecase using case mapping information from the UnicodeData file. |
| static int toTitleCase?(int codePoint) | This method converts the character (Unicode code point) argument to titlecase using case mapping information from the UnicodeData file. |
| static Character valueOf?(char c) | This method returns a Character instance representing the specified char value. |