Class Character in Java
Character class provides us a value of the primitive type Character(char) in an object. An object of type Character contains a single field whose type is char. Character class provides an immutable object wrapper around the primitive char data type. charValue () returns the char value of a character object.
This class- Character is useful when we need to treat a char value as an object. Several methods take an Object reference as an input. For them, this is the ideal solution. This class also supports the Reflection API and class literals.
In Java, Character objects represent values defined by the Unicode standard. Unicode is defined by an organization called the Unicode Consortium. The defining document for Unicode is The Unicode Standard, Version 2.0 (ISBN 0-201-48345-9).
Several class methods provide the Java/ Unicode equivalent of the C<ctype.h> character macros for checking the type of characters and converting them to uppercase and lowercase letters.
getType() method returns the character type information. The return value is one of the constants defined by the class, which represents several broad Unicode character categories.digit() returns the integer equivalent of a given character for a given radix (eg, radix 16 for hexadecimal). fordigit() returns the character that corresponds to the specified value for the specified radix.
Some of the methods in the Character class are concerned with characters that are digits; these characters are used by several other classes to convert strings that contain numbers into actual numeric values. The digit-related methods all use a radix value to interpret characters.
The radix is the numeric base used to represent numbers as characters or strings. Octal is a radix 8 representation, while hexadecimal is a radix 16 representation. The methods that require a radix parameter use it to determine which characters should be treated as valid digits.
In radix 2, only the characters `0′ and `1′ are valid digits. In radix 16, the characters ‘0’ through ‘9’, ‘a’ through ‘z’, and ‘A’ through ‘Z’ are considered valid digits.
The class Character is defined as :
public final class java.lang.Character extends java.lang.Object implements java.io.Serializable{
//Member Elements
public final static int MAX_RADIX;
public final static char MAX_VLUE;
public final static int MIN_RADIX;
public final static char MIN_VLUE;
public static final Class TYPE;
//character type constants
public static final byte COMBINING_SPACING _ MARK;
public static final byte CONNECTOR_PUNCTUATION;
public static final byte CONTROL;
public static final byte CURRENCY_SYMBOL;
public static final byte DASH_PUNCTUATION;
public static final byte DECIMAL_DIGIT_NUMBER;
public static final byte ENCLOSING_MARK;
public static final byte END PUNCTUATION;
public static final byte FORMAT;
public static final byte LETTER_NUMBER;
public static final byte LINE_SEPARATOR;
public static final byte LOWERCASE_LETTER;
public static final byte MATH_SYMBOL;
public static final byte MODIFIER_LETTER;
public static final byte MODIFIER_SYMBOL;
public static final byte NON_SPACING_MARK;
public static final byte OTHER_LETTER;
public static final byte OTHER_NUMBER;
public static final byte OTHER_PUNCTUATION;
public static final byte OTHER_SYMBOL;
public static final byte PARAGRAPH_SEPARATOR;
public static final byte PRIVATE_USE;
public static final byte SPACE_SEPARATOR;
public static final byte START_PUNCTUATION
public static final byte SURROGATE;
public static final byte TITLECASE_LETTER;
public static final byte UNASSIGNED;
public static final byte UPPERCASE_LETTER;
//constructor
public character(Char value)
//Methods:
public char charValue();
public static int digit(char ch,int redix);
public boolean equals(Object obj);
public static char forDigit(int digit,int redix);
public int hashCode();
public static char toLowerCase(char ch);
public static char toTtleCase(char ch);
public static char toUpperCase(char ch);
public String toString();
public static boolean isDefined(char ch);
public static boolean isDigit(char ch);
public static boolean isJavaLetter(char ch);
public static boolean isJavaLetterorDigit(char ch);
public static boolean isLetter(char ch);
public static boolean isLetterorDigit(char ch);
public static boolean isLowerCase(char ch);
public static boolean isSpace(char ch);
public static boolean isTitleCase(char ch);
public static boolean isUpperCase(char ch);
public static int getNumericValue(char ch);
public static int getType(char ch);
public static boolean isISOControl(char ch):
public static boolean isIdentifierIgnorable(char ch);
public static boolean isJavaIdentifierPart(char ch);
public static boolean isJavaIdentifierStart(char ch);
public static boolean isSpaceChar(char ch);
public static boolean isUnicodeIdentifierPart(char ch);
public static boolean isUnicodeIdentifierStart(char ch);
public static boolean isWhitespace(char ch);
}
The details of these are given as:
public final static int MAX_RADIX;
The constant value of this field is the largest value(36) permitted for the radix argument in the radix conversion method such as the digit method, the forDigit() method and the toString() method of the class Integer.
public final static char MAX_VLUE;
The constant value of this field is the largest value like-‘uffff’ for type char
public final static int MIN_RADIX;
The constant value of this field is the smallest value permitted for the radix argument in the radix conversion method such as the digit method, the forDigit() method and the toString() method of the class Integer.
public final static char MIN_VLUE;
The constant value of this field is the smallest value like-‘u0000’ for type char
public static final Class TYPE;
The Class object that represents the type char. It is always true that Character.TYPE == char.class.
public static final byte COMBINING_SPACING _ MARK;
This constant can be returned by the getType() method as the general category of a Unicode character.
public static final byte CONNECTOR_PUNCTUATION;
This constant can be returned by the getType() method as the general category of a Unicode character.
public static final byte CONTROL;
This constant can be returned by the getType() method as the general category of a Unicode character.
public static final byte CURRENCY_SYMBOL;
This constant can be returned by the getType() method as the general category of a Unicode character.
public static final byte DASH_PUNCTUATION;
This constant can be returned by the getType() method as the general category of a Unicode character.
public static final byte DECIMAL_DIGIT_NUMBER;
This constant can be returned by the getType() method as the general category of a Unicode character.
public static final byte ENCLOSING_MARK;
This constant can be returned by the getType() method as the general category of a Unicode character.
public static final byte END PUNCTUATION;
This constant can be returned by the getType() method as the general category of a Unicode character.
public static final byte FORMAT;
This constant can be returned by the getType() method as the general category of a Unicode character.
public static final byte LETTER_NUMBER;
This constant can be returned by the getType() method as the general category of a Unicode character.
public static final byte LINE_SEPARATOR;
This constant can be returned by the getType() method as the general category of a Unicode character.
public static final byte LOWERCASE_LETTER;
This constant can be returned by the getType() method as the general category of a Unicode character.
public static final byte MATH_SYMBOL;
This constant can be returned by the getType() method as the general category of a Unicode character.
public static final byte MODIFIER_LETTER;
This constant can be returned by the getType() method as the general category of a Unicode character.
public static final byte MODIFIER_SYMBOL;
This constant can be returned by the getType() method as the general category of a Unicode character.
public static final byte NON_SPACING_MARK;
This constant can be returned by the getType() method as the general category of a Unicode character.
public static final byte OTHER_LETTER;
This constant can be returned by the getType() method as the general category of a Unicode character.
public static final byte OTHER_NUMBER;
This constant can be returned by the getType() method as the general category of a Unicode character.
public static final byte OTHER_PUNCTUATION;
This constant can be returned by the getType() method as the general category of a Unicode character.
public static final byte OTHER_SYMBOL;
This constant can be returned by the getType() method as the general category of a Unicode character.
public static final byte PARAGRAPH_SEPARATOR;
This constant can be returned by the getType() method as the general category of a Unicode character.
public static final byte PRIVATE_USE;
This constant can be returned by the getType() method as the general category of a Unicode character.
public static final byte SPACE_SEPARATOR;
This constant can be returned by the getType() method as the general category of a Unicode character.
public static final byte START_PUNCTUATION
This constant can be returned by the getType() method as the general category of a Unicode character.
public static final byte SURROGATE;
This constant can be returned by the getType() method as the general category of a Unicode character.
public static final byte TITLECASE_LETTER;
This constant can be returned by the getType() method as the general category of a Unicode character.
public static final byte UNASSIGNED;
This constant can be returned by the getType() method as the general category of a Unicode character.
public static final byte UPPERCASE_LETTER;
This constant can be returned by the getType() method as the general category of a Unicode character.
public character(Char value)
Creates a character object and initializes it so that it can represent the value passed in the argument. The argument char value will be encapsulated by this object.
public char charValue();
charValue() method returns the char value contained by the object.
public static int digit(char ch,int redix);
This method returns the numeric value represented by a digit character. For example, digit(‘7’,10) returns 7. If the value of ch is not a valid digit, the method returns -1. For example, digit(‘7’,2) returns -1 because ‘7’ is not a valid digit in radix 2.
Several methods in other classes use this method to convert strings that contain numbers to actual numeric values. The forDigit() method is an approximate inverse of this method.
If radix is greater than 10, characters in the range `A’ to ‘A’+radix-11 are treated as valid digits. Such a character has the numeric value ch-‘A’+10. By the same token, if the radix is greater than 10, characters in the range ‘a’ to ‘a’+radix-11 are treated as valid digits. Such a character has the numeric value ch-‘a’+10.
This method returns the numeric value of the digit. This method returns -1 if the value of ch is not considered a valid digit, if the radix is less than MIN_RADIX, or if the radix is greater than MAX_RADIX.
Parameters
ch– A char value that is a legal digit in the given radix.
radix– The radix used in interpreting the specified character as a digit. If radix is in the range 2 through 10, only characters for which the isDigit() method returns true are considered to be valid digits. If radix is in the range 11 through 36, characters in the ranges ‘A’ through ‘Z’ and ‘a’ through ‘z’ may be considered valid digits.
public boolean equals(Object obj);
equals() method returns true if obj is an instance of Character, and it contains the same value as the object this method is associated with. It overrides Object.equals() method. It returns true if the objects are equal; false if they are not.
Parameter-The object to be compared with this object.
public static char forDigit(int digit,int redix);
This method returns the character that represents the digit corresponding to the specified numeric value. If the digit is in the range 0 through 9, the method returns ‘0’+digit. If the digit is in the range 10 through MAX_RADIX-1, the method returns ‘a’+digit-10. The method returns ‘\ 0’ if the digit is less than 0, if the digit is equal to or greater than radix, if the radix is less than MIN_RADIX, or if the radix is greater than MAX_RADIX.
This method returns the character that represents the digit corresponding to the specified numeric value. The method returns ‘\0’ if the digit is less than 0, if the digit is equal to or greater than radix, if the radix is less than MIN_RADIX, or if the radix is greater than MAX_RADIX.
Parameters
digit– The numeric value represented as a digit character.
radix– The radix used to represent the specified value.
public int hashCode();
hashCode() method returns a hashcode based on the char value of the object. It overrides Object.hashCode() method.
public static char toLowerCase(char ch);
toLowerCase() method returns the lowercase equivalent of the specified character value. If the specified character is not uppercase or if it has no lowercase equivalent, the character is returned unmodified. The Unicode attribute table determines if a character has a mapping to a lowercase equivalent.
Some Unicode characters in the range \u2000 through \u2FFF have lowercase mappings. For example,\u2160 (Roman numeral one) has a lowercase mapping to \u2170 (small Roman numeral one). The toLowerCase() method maps such characters to their lowercase equivalents even though the method isUpperCase() does not return true for such characters.
This method returns The lowercase equivalent of the specified character, or the character itself if it cannot be converted to lowercase
Parameters
ch -A char value
public static char toTtleCase(char ch);
toTtleCase() method returns the titlecase equivalent of the specified character value. If the specified character has no titlecase equivalent, the character is returned unmodified. The Unicode attribute table is used to determine the character’s titlecase equivalent.
Many characters are defined by the Unicode standard as having upper- and lowercase forms. There are some characters defined by the Unicode standard that also have a titlecase form. The glyphs for these characters look like a combination of two Latin letters.
The titlecase form of these characters has a glyph that looks like a combination of an uppercase Latin character and a lowercase Latin character; this case should be used when the character appears as the first character of a word in a title.
For example, one of the Unicode characters that have a titlecase form like the letter ‘D’ followed by the letter ‘Z’. Here is what the three forms of this letter look like:
Character | Meaning |
---|---|
Uppercase | ‘DZ’ |
Titlecase | ‘Dz’ |
Lowercase | ‘dz’ |
This method returns the titlecase equivalent of the specified character, or the character itself if it cannot be converted to titlecase.
Parameters
ch- A char value
public static char toUpperCase(char ch);
toUpperCase() method returns the uppercase equivalent of the specified character value. If the specified character is not lowercase or if it has no uppercase equivalent, the character is returned unmodified. The Unicode attribute table determines if a character has a mapping to an uppercase equivalent.
Some Unicode characters in the range \u2000 through \u2FFF have uppercase mappings. For example, \u2170 (small Roman numeral one) has a lowercase mapping to \u2160 (Roman numeral one). The toUpperCase() method maps such characters to their uppercase equivalents even though the method isLowerCase() does not return true for such characters.
This method returns the uppercase equivalent of the specified character, or the character itself if it cannot be converted to uppercase.
Parameters
ch -A char value
public String toString();
toString() method returns a string representation of the Character object. hence it returns a String of length one that contains the character value of the object. It overrides Object.toString() method.
Parameters
ch- A char value
public static boolean isDefined(char ch);
is definedisDefined method returns true if the specified character value has an assigned meaning in the Unicode character set. It returns true if the specified character has an assigned meaning in the Unicode character set; otherwise false.
Parameters
ch -A char value that needs to be examined.
public static boolean isDigit(char ch);
isDigit() method determines whether or not the specified character is a digit, based on the definition of the character in Unicode. It returns true if the specified character is defined as a digit in the Unicode character set; otherwise false.
Parameters
ch -A char value
public static boolean isJavaLetter(char ch);
isJavaLetter() method returns true if the specified character can appear as the first character in a Java identifier.
A character is considered a Java letter if and only if it is a letter, the character $, or the character _.
This method returns false for digits because digits are not allowed as the first character of an identifier.
This method returns true if the specified character can appear as the first character in a Java identifier; otherwise false.
Parameters
ch -A char value
public static boolean isJavaLetterorDigit(char ch);
isJavaLetterorDigit() method returns true if the specified character can appear in a Java identifier after the first character.
A character is considered a Java letter or digit if and only if it is a letter, a digit, the character $, or the character _.
This method returns true if the specified character can appear after the first character in a Java identifier; otherwise false.
Parameters
ch -A char value
public static boolean isLetter(char ch);
isLetter() method determines whether or not the specified character is a letter, based on the definition of the
character in Unicode. This method does not consider character values in ranges that have not been assigned meanings by Unicode to be letters.
This method returns true if the specified character is defined as a letter in the Unicode character set; otherwise false.
Parameters
ch- A char value
public static boolean isLetterorDigit(char ch);
isLetterorDigit() method determines whether or not the specified character is a letter or a digit, based on the definition of the character in Unicode.
There are some ranges that have not been assigned meanings by Unicode. If a character value is in one of these ranges, this method does not consider the character to be a letter.
This method returns true if the specified character is defined as a letter in the Unicode character set; otherwise false.
Parameters
ch -A char value
public static boolean isLowerCase(char ch);
isLowerCase() method determines whether or not the specified character is lowercase. Unicode defines a number of characters that do not have case mappings; if the specified character is one of these characters, the method returns false.
This method returns true if the specified character is defined as lowercase in the Unicode character set; otherwise false.
Parameters
ch -A char value
public static boolean isSpace(char ch);
isSpace() method determines whether or not the specified character is whitespace. This method recognizes the whitespace characters shown in the following table.
Character | Meaning |
---|---|
\u0009 | Horizontal tab |
\u000A | Newline |
\u000C | Formfeed |
\u000D | Carriage return |
\u0020 | ” space |
This method returns true if the specified character is defined as whitespace in the ISO-Latin-1 character set; otherwise false.
Parameters
ch- A char value
public static boolean isTitleCase(char ch);
isTitleCase() method determines whether or not the specified character is a titlecase character. Unicode defines a number of characters that do not have case mappings; if the specified character is one of these characters, the method returns false.
Many characters are defined by the Unicode standard as having upper- and lowercase forms. There are some characters defined by the Unicode standard that also have a titlecase form. The glyphs for these characters look like a combination of two Latin letters.
The titlecase form of these characters has a glyph that looks like a combination of an uppercase Latin character and a lowercase Latin character; this case should be used when the character appears as the first character of a word in a title.
For example, one of the Unicode characters that have a titlecase form looks like the letter ‘D’ followed by the letter ‘Z’. Here is what the three forms of this letter look like:
Character | Meaning |
---|---|
Uppercase | ‘DZ’ |
Titlecase | ‘Dz’ |
Lowercase | ‘dz’ |
Parameters
ch -A char value
public static boolean isUpperCase(char ch);
isUpperCase() method determines whether or not the specified character is uppercase. Unicode defines a number of characters that do not have case mappings; if the specified character is one of these characters, the method returns false.
This method returns true if the specified character is defined as uppercase in the Unicode character set; otherwise false.
Parameters
ch -A char value
public static int getNumericValue(char ch);
getNumericValue() method returns the Unicode numeric value of the specified character as a non-negative integer.
It returns the Unicode numeric value of the character as a non-negative integer. This method returns -1 if the character has no numeric value; it returns -2 if the character has a numeric value that is not a nonnegative integer, such as 1/2.
Parameters
ch A char value
public static int getType(char ch);
getType This method returns the Unicode general category type of the specified character. The value corresponds to one of the general category constants defined by Character.
This method returns An int value that represents the Unicode general category type of the character.
Parameters
ch – A char value
public static boolean isISOControl(char ch):
isISOControl() method determines whether or not the specified character is an ISO control character. A character is an ISO control character if it falls in the range \u0000 through \u001F or \u007F through \u009F.
This method returns true if the specified character is an ISO control character; otherwise false.
Parameters
ch- A char value
public static boolean isIdentifierIgnorable(char ch);
isIdentifierIgnorable() method determines whether or not the specified character is ignorable in a Java or Unicode identifier.The following characters are ignorable in a Java or Unicode identifier:
Character | Meaning |
---|---|
\u0000 – \u0008 \u000E – \u001B \u007F – \u009F | ISO control characters that aren’t whitespace |
\u200C – \u200F | Join controls |
\u200A – \u200E | Bidirectional controls |
\u206A – \u206F | Format controls |
\uFEFF | Zero-width no-break space |
This method returns true if the specified character is ignorable in a Java or Unicode identifier; otherwise false.
Parameters
ch A char value
public static boolean isJavaIdentifierPart(char ch);
isJavaIdentifierPart() method returns true if the specified character can appear in a Java identifier after the first character.
A character is considered part of a Java identifier if and only if it is a letter, a digit, a currency symbol (e.g. $), a connecting punctuation character (e.g. _), a numeric letter (e.g. a Roman numeral), a combining mark, a nonspacing mark, or an ignorable control character.
This method returns true if the specified character can appear after the first character in a Java identifier; otherwise false.
Parameters
ch A char value
public static boolean isJavaIdentifierStart(char ch);
isJavaIdentifierStart() method returns true if the specified character can appear in a Java identifier as the first character.
A character is considered a start of a Java identifier if and only if it is a letter, a currency symbol (e.g., $), or a connecting punctuation character (e.g., _).
It returns true if the specified character can appear as the first character in a Java identifier; otherwise false.
Parameters
ch A char value
public static boolean isSpaceChar(char ch);
isSpaceChar() method determines if the specified character is a space character according to the Unicode 2.0 specification. A character is considered to be a Unicode space character if and only if it has a general category “Zs”, “Zl”, or “Zp” in the Unicode specification.
This method returns true if the specified character is a Unicode 2.0 space characters; otherwise false.
Parameters
ch A char value
public static boolean isUnicodeIdentifierPart(char ch);
isUnicodeIdentifierPart() method returns true if the specified character can appear in a Unicode identifier after the first character.
A character is considered part of a Unicode identifier if and only if it is a letter, a digit, a connecting punctuation character (e.g., _), a numeric letter (e.g., a Roman numeral), a combining mark, a nonspacing mark, or an ignorable control character.
This method returns true if the specified character can appear after the first character in a Unicode identifier; otherwise false.
Parameters
ch A char value
public static boolean isUnicodeIdentifierStart(char ch);
isUnicodeIdentifierStart method returns true if the specified character can appear in a Unicode identifier as the first character.
A character is considered the start of a Unicode identifier if and only if it is a letter.
This method returns true if the specified character can appear as the first character in a Unicode identifier; otherwise false.
Parameters
ch A char value
public static boolean isWhitespace(char ch);
isWhitespace() method This method determines whether or not the specified character is whitespace. This method recognizes the following as whitespace
Character | Meaning |
---|---|
Unicode category “Zs” except \u00A0 and \uFEFF | Unicode space separators except for no-break spaces |
Unicode category “Zl” | Unicode line separators |
Unicode category “Zp” | Unicode paragraph separators |
\u0009 | Horizontal tab |
\u000A | Linefeed |
\u000B | Vertical tab |
\u000C | Formfeed |
\u000D | Carriage return |
\u001C | File Separator |
\u001D | Group Separator |
\u001E | Record Separator |
\u001F | Unit Separator |
This method returns true if the specified character is defined as whitespace according to Java; otherwise false.
Parameters
ch A char value
Apart from these methods this class also has other inherited methods from class Object. They are as follows:
- clone()
- getClass()
- notifyAll()
- finalize()
- notify()
- wait(long)
- wait()
- wait(long, int)