Open In App

Character Class in Java

Last Updated : 14 Feb, 2022
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Java provides a wrapper class Character in java.lang package. An object of type Character contains a single field, whose type is char. The Character class offers a number of useful class (i.e., static) methods for manipulating characters. You can create a Character object with the Character constructor.

Creating a Character object: 

Character ch = new Character('a');

The above statement creates a Character object which contains ‘a’ of type char. There is only one constructor in the Character class that expects an argument of char data type.

If we pass a primitive char into a method that expects an object, the compiler automatically converts the char to a Character class object. This feature is called Autoboxing and Unboxing.

Note: The Character class is immutable like String class i.e once it’s object is created, it cannot be changed.

Methods in Character Class  

The methods of Character class are as follows:

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

Returns: It returns true if ch is an alphabet, otherwise, return false

Example:

Java




// Java program to demonstrate isLetter() method
  
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)

Parameters: 

  • ch – a primitive character

Returns: It returns true if ch is a digit, otherwise, return false

Example:

Java




// Java program to demonstrate isDigit() method
  
public class Test {
    public static void main(String[] args)
    {
        // print false as A is character
        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)

Parameters: 

  • ch – a primitive character

Returns: It returns true if ch is whitespace, otherwise, returns false.

Example:

Java




// Java program to demonstrate isWhitespace() method
  
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'));
  
        // ASCII value of tab
        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)

Parameters: 

  • ch – a primitive character

Returns: It returns true if ch is upper case, otherwise, returns false.

Example:

Java




// Java program to demonstrate isUpperCase() method
  
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)

Parameters: 

  • ch – a primitive character

Returns: It returns true if ch is lower case, otherwise, returns false.

Example:

Java




// Java program to demonstrate isLowerCase() method
  
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

Returns: It returns the uppercase form of the specified char value.

Example:

Java




// Java program to demonstrate toUpperCase() method
  
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)

Parameters: 

  • ch – a primitive character

Returns: It returns the lowercase form of the specified char value.

Example:

Java




// Java program to demonstrate toLowerCase() method
  
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)

Parameters: 

  • ch – a primitive character

Returns: It returns a String object.

Example:

Java




// Java program to demonstrate toString() method
  
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

S. No. Method Description
1. static int charCount?(int codePoint) This method determines the number of char values needed to represent the specified character (Unicode code point).
2. char charValue?() This method returns the value of this Character object.
3. static int codePointAt?(char[] a, int index) This method returns the code point at the given index of the char array.
4. 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.
5. static int codePointAt?(CharSequence seq, int index) This method returns the code point at the given index of the CharSequence.
6. static int codePointBefore?(char[] a, int index) This method returns the code point preceding the given index of the char array.
7. 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.
8. static int codePointBefore?(CharSequence seq, int index) This method returns the code point preceding the given index of the CharSequence.
9. 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.
10. 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.
11. static int codePointOf?(String name) This method returns the code point value of the Unicode character specified by the given Unicode character name.
12. static int compare?(char x, char y) This method compares two char values numerically.
13. int compareTo?(Character anotherCharacter) This method compares two Character objects numerically.
14. static int digit?(char ch, int radix) This method returns the numeric value of the character ch in the specified radix.
15. static int digit?(int codePoint, int radix) This method returns the numeric value of the specified character (Unicode code point) in the specified radix.
16. boolean equals?(Object obj) This method compares this object against the specified object.
17. static char forDigit?(int digit, int radix) This method determines the character representation for a specific digit in the specified radix.
18. static byte getDirectionality?(char ch) This method returns the Unicode directionality property for the given character.
19. static byte getDirectionality?(int codePoint) This method returns the Unicode directionality property for the given character (Unicode code point).
20. static String getName?(int codePoint) This method returns the Unicode name of the specified character codePoint, or null if the code point is unassigned.
21. static int getNumericValue?(char ch) This method returns the int value that the specified Unicode character represents.
22. static int getNumericValue?(int codePoint) This method returns the int value that the specified character (Unicode code point) represents.
23. static int getType?(char ch) This method returns a value indicating a character’s general category.
24. static int getType?(int codePoint) This method returns a value indicating a character’s general category.
25. int hashCode?() This method returns a hash code for this Character; equal to the result of invoking charValue().
26. static int hashCode?(char value) This method returns a hash code for a char value; compatible with Character.hashCode().
27. 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.
28. static boolean isAlphabetic?(int codePoint) This method determines if the specified character (Unicode code point) is an alphabet.
29. static boolean isBmpCodePoint?(int codePoint) This method determines whether the specified character (Unicode code point) is in the Basic Multilingual Plane (BMP).
30. static boolean isDefined?(char ch) This method determines if a character is defined in Unicode.
31. static boolean isDefined?(int codePoint) This method determines if a character (Unicode code point) is defined in Unicode.
32. 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).
33. 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.
34. 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.
35. 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.
36. static boolean isISOControl?(char ch) This method determines if the specified character is an ISO control character.
37. static boolean isISOControl?(int codePoint) This method determines if the referenced character (Unicode code point) is an ISO control character.
38. 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.
39. 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.
40. static boolean isJavaIdentifierStart?(char ch) This method determines if the specified character is permissible as the first character in a Java identifier.
41. static boolean isJavaIdentifierStart?(int codePoint) This method determines if the character (Unicode code point) is permissible as the first character in a Java identifier.
42. 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).
43. static boolean isLetterOrDigit?(char ch) This method determines if the specified character is a letter or digit.
44. static boolean isLetterOrDigit?(int codePoint) This method determines if the specified character (Unicode code point) is a letter or digit.
45. static boolean isMirrored?(char ch) This method determines whether the character is mirrored according to the Unicode specification.
46. static boolean isMirrored?(int codePoint) This method determines whether the specified character (Unicode code point) is mirrored according to the Unicode specification.
47. static boolean isSpaceChar?(char ch) This method determines if the specified character is a Unicode space character.
48. static boolean isSpaceChar?(int codePoint) This method determines if the specified character (Unicode code point) is a Unicode space character.
49. static boolean isSupplementaryCodePoint?(int codePoint) This method determines whether the specified character (Unicode code point) is in the supplementary character range.
50. static boolean isSurrogate?(char ch) This method determines if the given char value is a Unicode surrogate code unit.
51. static boolean isSurrogatePair?(char high, char low) This method determines whether the specified pair of char values is a valid Unicode surrogate pair.
52. static boolean isTitleCase?(char ch) This method determines if the specified character is a titlecase character.
53. static boolean isTitleCase?(int codePoint) This method determines if the specified character (Unicode code point) is a titlecase character.
54. 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.
55. 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.
56. static boolean isUnicodeIdentifierStart?(char ch) This method determines if the specified character is permissible as the first character in a Unicode identifier.
57. 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.
58. static boolean isValidCodePoint?(int codePoint) This method determines whether the specified code point is a valid Unicode code point value.
59. 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.
60. 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.
61. 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.
62. static char reverseBytes?(char ch) This method returns the value obtained by reversing the order of the bytes in the specified char value.
63. static char[] toChars?(int codePoint) This method converts the specified character (Unicode code point) to its UTF-16 representation stored in a char array.
64. static int toChars?(int codePoint, char[] dst, int dstIndex) This method converts the specified character (Unicode code point) to its UTF-16 representation.
65. static int toCodePoint?(char high, char low) This method converts the specified surrogate pair to its supplementary code point value.
66. static char toTitleCase?(char ch) This method converts the character argument to titlecase using case mapping information from the UnicodeData file.
67. static int toTitleCase?(int codePoint) This method converts the character (Unicode code point) argument to titlecase using case mapping information from the UnicodeData file.
68. static Character valueOf?(char c) This method returns a Character instance representing the specified char value.

Escape Sequences: 

A character preceded by a backslash (\) is an escape sequence and has special meaning to the compiler. The following table shows the Java escape sequences: 

Escape Sequence Description
\t Insert a tab in the text at this point.
\b Insert a backspace in the text at this point.
\n Insert a newline in the text at this point.
\r Insert a carriage return in the text at this point.
\f Insert a formfeed in the text at this point.
\’ Insert a single quote character in the text at this point.
\” Insert a double quote character in the text at this point.
\\ Insert a backslash character in the text at this point.

When an escape sequence is encountered in a print statement, the compiler interprets it accordingly. For example, if you want to put quotes within quotes you must use the escape sequence, \”, on the interior quotes. To print the sentence

She said "Hello!" to me.

you would write

System.out.println("She said \"Hello!\" to me.");



Previous Article
Next Article

Similar Reads

Java.lang.Character.Subset Class in Java
Character.Subset Class represents particular subsets of the Unicode(standards using hexadecimal values to express characters - 16bit) character set. The subset, it defines in Character set is UnicodeBlock. Declaration : public static class Character.Subset extends Object Constructors : protected Character.Subset(String str) : Constructs new subset
2 min read
Java.lang.Character.UnicodeBlock Class in Java
Character.UnicodeBlock Class represents particular Character blocks of the Unicode(standards using hexadecimal values to express characters - 16 bit) specifications. Character Blocks define characters used for specific purpose. Declaration : public static final class Character.UnicodeBlock extends Character.Subset Methods of Character.UnicodeBlock
2 min read
Find the count of M character words which have at least one character repeated
Given two integers N and M, the task is count the total words of M character length formed by the given N distinct characters such that the words have at least one character repeated more than once. Examples: Input: N = 3, M = 2 Output: 3 Suppose the characters are {'a', 'b', 'c'} All 2 length words that can be formed with these characters are "aa"
4 min read
java.lang.Character class methods | Set 1
[caption id="attachment_151090" align="aligncenter" width="787"] lang.Character class wraps the value of a primitive data type - char to an object of datatype char and this object contains a single field having the data type - char. This class provides no. of methods regarding character manipulations like converting them from lowercase to uppercase
7 min read
java.lang.Character class - methods | Set 2
java.lang.Character class - methods | Set 1 Following methods of Character class are discussed here : toUpperCase() : java.lang.toUpperCase(char arg) method converts given character in argument to its Upper case based on Unicode values. Syntax : public static char toUpperCase(char arg) Parameters : arg : character to be converted to Upper case Retu
4 min read
Java.lang.Class class in Java | Set 1
Java provides a class with name Class in java.lang package. Instances of the class Class represent classes and interfaces in a running Java application. The primitive Java types (boolean, byte, char, short, int, long, float, and double), and the keyword void are also represented as Class objects. It has no public constructor. Class objects are cons
15+ min read
Java.lang.Class class in Java | Set 2
Java.lang.Class class in Java | Set 1 More methods: 1. int getModifiers() : This method returns the Java language modifiers for this class or interface, encoded in an integer. The modifiers consist of the Java Virtual Machine's constants for public, protected, private, final, static, abstract and interface. These modifiers are already decoded in Mo
15+ min read
Difference Between java.sql.Time, java.sql.Timestamp and java.sql.Date in Java
Across the software projects, we are using java.sql.Time, java.sql.Timestamp and java.sql.Date in many instances. Whenever the java application interacts with the database, we should use these instead of java.util.Date. The reason is JDBC i.e. java database connectivity uses these to identify SQL Date and Timestamp. Here let us see the differences
7 min read
Java Character charCount() with Examples
The java.lang.Character.charCount() is an inbuilt function in java which is used to determine number of characters needed to represent the specified character. If the character is equal to or greater than 0x10000, the method returns 2 otherwise 1. Syntax: public static int charCount(int code) Parameters: The function accepts a single parameter code
2 min read
Character.charValue() in Java with examples
Java.lang.Character.charValue() is a built-in method in Java that returns the value of this character object. This method converts the Character object into its primitive data type char. Syntax: public char charValue() The function does not accepts any parameter. Return Type: This method returns the primitive char value represented by this object.
2 min read
Character.hashCode() in Java with examples
Java.lang.Character.hashCode() is a built-in method in Java which returns a hash code for this Character. The returned hash code is equal to the result of invoking charValue(). Syntax: public int hashCode() This function does not accepts any parameter. Return Value: This method returns a hash code value for this Character. Below programs illustrate
2 min read
Character.isMirrored() in Java with examples
java.lang.Character.isMirrored() is an inbuilt method in java that determines whether the character is mirrored according to the Unicode specification. Mirrored characters should have their glyphs horizontally mirrored when displayed in text that is right-to-left. For example, ‘\u0028’ LEFT PARENTHESIS is semantically defined to be an opening paren
2 min read
Character.reverseBytes() in Java with examples
java.lang.Character.reverseBytes() is a built-in method in java which returns the value obtained by reversing the order of the bytes in the specified char value. Syntax: public static char reverseBytes(char ch) Parameter: The function accepts one mandatory parameter ch which signifies the character whose order of bytes is to be reversed. Returns: T
2 min read
Character.isTitleCase() in Java with examples
The java.lang.Character.isTitleCase() in an inbuilt method in java which determines if the specified character is a titlecase character or not. A character is a titlecase character if its general category type, provided by getType(codePoint), is a TITLECASE_LETTER. Some characters look like pairs of Latin letters. For example, there is an uppercase
2 min read
Character.offsetByCodePoints() in Java with Examples
The Character.offsetByCodePoints(char[] a, int start, int count, int index, int codePointOffset) is an inbuilt method in Java that returns the index within the given char subarray that is offset from the given index by codePointOffset code points. The start and count arguments specify a subarray of the char array. Unpaired surrogates within the tex
2 min read
Character.offsetByCodePoints() Method in java
The Character.offsetByCodePoints(CharSequence seq, int index, int codePointOffset) is an inbuilt method in java that returns the index within the given char sequence that is offset from the given index by codePointOffset code points. Unpaired surrogates within the text range given by index and codePointOffset count as one code point each. Syntax: p
2 min read
Character.isUnicodeIdentifierPart() Method in Java with Examples
The java.lang.Character.isUnicodeIdentifierPart() is an inbuilt method in java that determines if the specified character may be part of a Unicode identifier as other than the first character. A character may be part of a Unicode identifier if and only if one of the following statements is true: it is a connecting punctuation character (such as '_'
4 min read
Character.equals() method in Java with examples
The java.lang.Character.equals() is a function in Java which compares this object against the specified object. If the argument is not null then the result is true and is a Character object that represents the same char value as this object. Syntax: public boolean equals(Object obj) Parameters: The function accepts a single parameter obj which spec
2 min read
Character.isMirrored() method in Java
The Character.isMirrored(int codePoint) is an inbuilt method in java that determines whether the specified character (Unicode code point) is mirrored according to the Unicode specification. Mirrored characters should have their glyphs horizontally mirrored when displayed in text that is right-to-left. For example, ‘\u0028’ LEFT PARENTHESIS is seman
2 min read
Character.getDirectionality() method in Java with examples
The java.lang.Character.getDirectionality() is an inbuilt method in Java which returns the Unicode directionality property for the given character. Character directionality is used to calculate the visual ordering of text. The directionality value of undefined char values is DIRECTIONALITY_UNDEFINED. This method cannot handle supplementary characte
2 min read
Character.isSupplementaryCodePoint() Method in Java
The java.lang.Character.isSupplementaryCodePoint(int codePoint) is an inbuilt method in java that determines whether the specified character (Unicode code point) is in the supplementary character range. Syntax: public static boolean isSupplementaryCodePoint(int codePoint) Parameters: The codePoint of integer type refers to the character (Unicode co
2 min read
Character.isISOControl() method with examples in Java
The java.lang.Character.isISOControl() is an inbuilt method in java which determines if the specified character is an ISO control character or not. A character is considered to be an ISO control character if its code is in the range ‘\u0000’ through ‘\u001F’ or in the range ‘\u007F’ through ‘\u009F’. This method cannot handle supplementary characte
3 min read
Character.isJavaIdentifierStart() Method in Java
The Character.isJavaIdentifierStart(int codePoint) is an inbuilt method in java that determines if the character (Unicode code point) is permissible as the first character in a Java identifier. It is to be noted that a character may start a Java identifier if and only if one of the following conditions is true: isLetter(ch) returns true getType(ch)
2 min read
Character.digit() in Java with examples
The java.lang.Character.digit() is an inbuilt method in java which returns the numeric value of the character ch in the specified radix. It returns -1 if the radix is not in the range MIN_RADIX or if the value of ch is not a valid digit in the specified radix. A character is a valid digit if at least one of the following is true: The method isDigit
3 min read
Character.isValidCodePoint() Method in Java with Examples
The Character.isValidCodePoint() is an inbuilt method in java that determines whether the specified code point mentioned in the parameter is a valid Unicode code point value or not. Syntax: public static boolean isValidCodePoint(int codePoint) Parameters: The parameter codePoint is of Integer datatype and refers to the unicode code point that is to
2 min read
Character.isJavaIdentifierPart() Method in Java with Examples
The Character.isJavaIdentifierPart(int codePoint) is an inbuilt method in java that determines if the specified character may be part of a Java identifier as other than the first character. A character may be a part of Java identifier if any of the following are true: it is a letter it is a currency symbol (such as ‘$’) it is a connecting punctuati
4 min read
Character.isLetterOrDigit() in Java with examples
The java.lang.Character.isLetterOrDigit(char ch) is an inbuilt method in java which determines if the specified character is a letter or digit. Syntax: public static boolean isLetterOrDigit(char ch) Parameters: The function accepts a single mandatory parameter ch which signifies the character to be tested. Return value: This function returns a bool
2 min read
Character.isWhitespace() method in Java with examples
The java.lang.Character.isWhitespace() is an inbuilt method in a java that determines if the specified character (Unicode code point) is white space according to Java. A character is a Java whitespace character if and only if it satisfies one of the following criteria: It is a Unicode space character (SPACE_SEPARATOR, LINE_SEPARATOR, or PARAGRAPH_S
3 min read
Character getType() Method in Java with examples
The Character.getType(char ch) is an inbuilt method in java that returns a value indicating a character’s general category. This method cannot handle supplementary characters. To support all Unicode characters, including supplementary characters, use the getType(int) method. Syntax:public static int getType(char ch)Parameters: The method accepts on
3 min read
Character.isJavaIdentifierStart() method in Java
The java.lang.Character.isJavaIdentifierStart(char ch) is an inbuilt method in java which determines if the specified character is permissible as the first character in a Java identifier or not. A character may start as a Java identifier if and only if one of the following conditions is true: isLetter(ch) returns true getType(ch) returns LETTER_NUM
2 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg