Character Class in Java

Rumman Ansari   Software Engineer   2019-03-30   7599 Share
☰ Table of Contents

Table of Content:


Java provides a wrapper class Character in java.lang package.

Character class wraps a value of the primitive type Java character in an object. An object of type Character contains a single field whose type is char.

what would be the benefit if wraps around the primitive data type to an object?

in development, we come across situations where we need to use objects instead of primitive data types. In order to achieve this, Java provides wrapper class Character for primitive data type char.
basically we would be able to access all the methods that is readily available on the Character class which is very useful. In addition, this class provides several methods for determining a character’s category (lowercase letter, digit, etc.) and for converting characters from uppercase to lowercase and vice versa.

Character information is based on the Unicode Standard, version 6.2.0.

Creating a Character object :

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 ?

 Character ch = new Character('b');
 

If you pass a primitive char into a method that expects an object, the compiler automatically converts the char to a Character for you. This feature is called autoboxing or unboxing, The above statement creates a Character object which contain 'b' of type char. There is only one constructor in Character class which expect an argument of char data type.

 // Here following primitive char 'b'
// is boxed into the Character object ch
Character ch = 'b';

// Here primitive 'p' is boxed for method test,
// return is unboxed to char 'c'
char c = test('p');
 

The Character class as part of the java.lang package is one of the classes of the java api that is being widely used.

There is only one constructor available to Character class which we can use to instantiate a Character object:

Constructor Description
Character(char value) Constructs a newly allocated Character object that represents the specified char value.

A character preceded by a backslash (\) is an escape sequence and has a special meaning to the compiler.

Following table shows the Java escape sequences ?

Escape Sequences

Escape Sequence Description
\t Inserts a tab in the text at this point.
\b Inserts a backspace in the text at this point.
\n Inserts a newline in the text at this point.
\r Inserts a carriage return in the text at this point.
\f Inserts a form feed in the text at this point.
\' Inserts a single quote character in the text at this point.
\" Inserts a double quote character in the text at this point.
\\ Inserts a backslash character in the text at this point.

Example of Escape Sequence

If you want to put quotes within quotes, you must use the escape sequence, \", on the interior quotes ?

public class EscapeSeq {

   public static void main(String args[]) {
      System.out.println("Example of \"Escape Sequence\" try it own.");
   }
}
 

Output

When an escape sequence is encountered in a print statement, the compiler interprets it accordingly.

 Example of "Escape Sequence" try it own.
Press any key to continue . . .
 

Equality of Character Class

Normally when we do equality test for Character primitive data type we do like this:

 	class Equality{

		public static void main(String args[])
		{
         Character char1= 'p';
   		 char char2 = 'p';
   		 if(char1==char2){
   			System.out.println("They are equal");
   		  }
   		else{
   			System.out.println("Values are not equal");
		}

	}
}
 
Output
  They are equal
Press any key to continue . . .
 

This will no longer work in dealing with Character object type. To test equality we need to make use of equals which is a method inherited from Object class.

	class Equality{

		public static void main(String args[])
		{
            Character char1 = 'p';
		   	Character char2 = new Character('c');
		   if(char1.equals(char2)){
		    	System.out.println("They are equal");
				}
			else{
			System.out.println("They are not equal");
		}

	}
}
 
Output
They are not equal
Press any key to continue . . .
 

This is correct result


Character Class Method Usage Examples

The java.lang.Character class includes many useful static methods with which text (or characters) can be manipulated. The other text manipulating classes are String, StringBuffer, StringTokenizer and StringBuilder.

Character Methods for beginners:

Method Description
boolean isLetter(char ch) Determines whether the specified char value is a letter.
boolean isDigit(char ch) Determines whether the specified char value is a digit.
boolean isWhitespace(char ch) Determines whether the specified char value is white space.
boolean isUpperCase(char ch) Determines whether the specified char value is uppercase.
boolean isLowerCase(char ch) Determines whether the specified char value is lowercase.
char toUpperCase(char ch) Returns the uppercase form of the specified char value.
char toLowerCase(char ch) Returns the lowercase form of the specified char value.
toString(char ch) Returns a String object representing the specified character value — that is, a one-character string.
forDigit(int num, int radix) Returns the digit character associated with the value of num.The radix of the conversion is specified by radix
digit(char digit, int radix) Returns the integer value associated with the specified character according to the specified radix.
isLetterOrDigit() Returns true if given character is a letter or a digit. Otherwise, it returns false.
isIdentifierIgnorable(char ch) Returns true if ch should be ignored in an identifier.Otherwise, it returns false.
isDefined(char ch) Returns true if ch is defined by Unicode.Otherwise, it returns false.

All Character Methods :

Modifier and Type Method and Description
static int charCount(int codePoint)
Determines the number of char values needed to represent the specified character (Unicode code point).
char charValue()
Returns the value of this Character object.
static int codePointAt(char[] a, int index)
Returns the code point at the given index of the char array.
static int codePointAt(char[] a, int index, int limit)
Returns the code point at the given index of the char array, where only array elements with index less than limit can be used.
static int codePointAt(CharSequence seq, int index)
Returns the code point at the given index of the CharSequence.
static int codePointBefore(char[] a, int index)
Returns the code point preceding the given index of the char array.
static int codePointBefore(char[] a, int index, int start)
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)
Returns the code point preceding the given index of the CharSequence.
static int codePointCount(char[] a, int offset, int count)
Returns the number of Unicode code points in a subarray of the char array argument.
static int codePointCount(CharSequence seq, int beginIndex, int endIndex)
Returns the number of Unicode code points in the text range of the specified char sequence.
static int compare(char x, char y)
Compares two char values numerically.
int compareTo(Character anotherCharacter)
Compares two Character objects numerically.
static int digit(char ch, int radix)
Returns the numeric value of the character ch in the specified radix.
static int digit(int codePoint, int radix)
Returns the numeric value of the specified character (Unicode code point) in the specified radix.
boolean equals(Object obj)
Compares this object against the specified object.
static char forDigit(int digit, int radix)
Determines the character representation for a specific digit in the specified radix.
static byte getDirectionality(char ch)
Returns the Unicode directionality property for the given character.
static byte getDirectionality(int codePoint)
Returns the Unicode directionality property for the given character (Unicode code point).
static String getName(int codePoint)
Returns the Unicode name of the specified character codePoint, or null if the code point is unassigned.
static int getNumericValue(char ch)
Returns the int value that the specified Unicode character represents.
static int getNumericValue(int codePoint)
Returns the int value that the specified character (Unicode code point) represents.
static int getType(char ch)
Returns a value indicating a character’s general category.
static int getType(int codePoint)
Returns a value indicating a character’s general category.
int hashCode()
Returns a hash code for this Character; equal to the result of invoking charValue().
static int hashCode(char value)
Returns a hash code for a char value; compatible with Character.hashCode().
static char highSurrogate(int codePoint)
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)
Determines if the specified character (Unicode code point) is an alphabet.
static boolean isBmpCodePoint(int codePoint)
Determines whether the specified character (Unicode code point) is in the Basic Multilingual Plane (BMP).
static boolean isDefined(char ch)
Determines if a character is defined in Unicode.
static boolean isDefined(int codePoint)
Determines if a character (Unicode code point) is defined in Unicode.
static boolean isDigit(char ch)
Determines if the specified character is a digit.
static boolean isDigit(int codePoint)
Determines if the specified character (Unicode code point) is a digit.
static boolean isHighSurrogate(char ch)
Determines if the given char value is a Unicode high-surrogate code unit (also known as leading-surrogate code unit).
static boolean isIdentifierIgnorable(char ch)
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)
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)
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)
Determines if the specified character is an ISO control character.
static boolean isISOControl(int codePoint)
Determines if the referenced character (Unicode code point) is an ISO control character.
static boolean isJavaIdentifierPart(char ch)
Determines if the specified character may be part of a Java identifier as other than the first character.
static boolean isJavaIdentifierPart(int codePoint)
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)
Determines if the specified character is permissible as the first character in a Java identifier.
static boolean isJavaIdentifierStart(int codePoint)
Determines if the character (Unicode code point) is permissible as the first character in a Java identifier.
static boolean isJavaLetter(char ch)
Deprecated. Replaced by isJavaIdentifierStart(char).
static boolean isJavaLetterOrDigit(char ch)
Deprecated. Replaced by isJavaIdentifierPart(char).
static boolean isLetter(char ch)
Determines if the specified character is a letter.
static boolean isLetter(int codePoint)
Determines if the specified character (Unicode code point) is a letter.
static boolean isLetterOrDigit(char ch)
Determines if the specified character is a letter or digit.
static boolean isLetterOrDigit(int codePoint)
Determines if the specified character (Unicode code point) is a letter or digit.
static boolean isLowerCase(char ch)
Determines if the specified character is a lowercase character.
static boolean isLowerCase(int codePoint)
Determines if the specified character (Unicode code point) is a lowercase character.
static boolean isLowSurrogate(char ch)
Determines if the given char value is a Unicode low-surrogate code unit (also known as trailing-surrogate code unit).
static boolean isMirrored(char ch)
Determines whether the character is mirrored according to the Unicode specification.
static boolean isMirrored(int codePoint)
Determines whether the specified character (Unicode code point) is mirrored according to the Unicode specification.
static boolean isSpace(char ch)
Deprecated. Replaced by isWhitespace(char).
static boolean isSpaceChar(char ch)
Determines if the specified character is a Unicode space character.
static boolean isSpaceChar(int codePoint)
Determines if the specified character (Unicode code point) is a Unicode space character.
static boolean isSupplementaryCodePoint(int codePoint)
Determines whether the specified character (Unicode code point) is in the supplementary character range.
static boolean isSurrogate(char ch)
Determines if the given char value is a Unicode surrogate code unit.
static boolean isSurrogatePair(char high, char low)
Determines whether the specified pair of char values is a valid Unicode surrogate pair.
static boolean isTitleCase(char ch)
Determines if the specified character is a titlecase character.
static boolean isTitleCase(int codePoint)
Determines if the specified character (Unicode code point) is a titlecase character.
static boolean isUnicodeIdentifierPart(char ch)
Determines if the specified character may be part of a Unicode identifier as other than the first character.
static boolean isUnicodeIdentifierPart(int codePoint)
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)
Determines if the specified character is permissible as the first character in a Unicode identifier.
static boolean isUnicodeIdentifierStart(int codePoint)
Determines if the specified character (Unicode code point) is permissible as the first character in a Unicode identifier.
static boolean isUpperCase(char ch)
Determines if the specified character is an uppercase character.
static boolean isUpperCase(int codePoint)
Determines if the specified character (Unicode code point) is an uppercase character.
static boolean isValidCodePoint(int codePoint)
Determines whether the specified code point is a valid Unicode code point value.
static boolean isWhitespace(char ch)
Determines if the specified character is white space according to Java.
static boolean isWhitespace(int codePoint)
Determines if the specified character (Unicode code point) is white space according to Java.
static char lowSurrogate(int codePoint)
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)
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)
Returns the index within the given char sequence that is offset from the given index by codePointOffset code points.
static char reverseBytes(char ch)
Returns the value obtained by reversing the order of the bytes in the specified char value.
static char[] toChars(int codePoint)
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)
Converts the specified character (Unicode code point) to its UTF-16 representation.
static int toCodePoint(char high, char low)
Converts the specified surrogate pair to its supplementary code point value.
static char toLowerCase(char ch)
Converts the character argument to lowercase using case mapping information from the UnicodeData file.
static int toLowerCase(int codePoint)
Converts the character (Unicode code point) argument to lowercase using case mapping information from the UnicodeData file.
String toString()
Returns a String object representing this Character’s value.
static String toString(char c)
Returns a String object representing the specified char.
static char toTitleCase(char ch)
Converts the character argument to titlecase using case mapping information from the UnicodeData file.
static int toTitleCase(int codePoint)
Converts the character (Unicode code point) argument to titlecase using case mapping information from the UnicodeData file.
static char toUpperCase(char ch)
Converts the character argument to uppercase using case mapping information from the UnicodeData file.
static int toUpperCase(int codePoint)
Converts the character (Unicode code point) argument to uppercase using case mapping information from the UnicodeData file.
static Character valueOf(char c)
Returns a Character instance representing the specified char value.