Class Character
- java.lang.Object
-
- java.lang.Character
-
- All Implemented Interfaces:
Serializable,Comparable<Character>
public final class Character extends Object implements Serializable, Comparable<Character>
TheCharacterclass wraps a value of the primitive typecharin an object. An object of typeCharactercontains a single field whose type ischar.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.0.0.
- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description static intMAX_RADIXThe maximum radix available for conversion to and from strings.static charMAX_VALUEThe constant value of this field is the largest value of typechar,'\uFFFF'.static intMIN_RADIXThe minimum radix available for conversion to and from strings.static charMIN_VALUEThe constant value of this field is the smallest value of typechar,'\u0000'.static intSIZEThe number of bits used to represent acharvalue in unsigned binary form, constant16.
-
Constructor Summary
Constructors Constructor Description Character(char value)Constructs a newly allocatedCharacterobject that represents the specifiedcharvalue.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description charcharValue()Returns the value of thisCharacterobject.static intcompare(char x, char y)Compares twocharvalues numerically.intcompareTo(Character anotherCharacter)Compares twoCharacterobjects numerically.static intdigit(char ch, int radix)Returns the numeric value of the character ch in the specified radix.booleanequals(Object obj)Compares this object against the specified object.static charforDigit(int digit, int radix)Determines the character representation for a specific digit in the specified radix.inthashCode()Returns a hash code for thisCharacter; equal to the result of invokingcharValue().static booleanisDigit(char ch)Determines if the specified character is a digit.static booleanisISOControl(char ch)Determines if the specified character is an ISO control character.static booleanisLowerCase(char ch)Determines if the specified character is a lowercase character.static booleanisSpaceChar(char ch)Determines if the specified character is a Unicode space character.static booleanisUpperCase(char ch)Determines if the specified character is an uppercase character.static booleanisWhitespace(char ch)Determines if the specified character is white space according to Java.static chartoLowerCase(char ch)Converts the character argument to lowercase using case mapping information from the UnicodeData file.StringtoString()Returns aStringobject representing thisCharacter's value.static StringtoString(char c)Returns aStringobject representing the specifiedchar.static chartoUpperCase(char ch)Converts the character argument to uppercase using case mapping information from the UnicodeData file.static CharactervalueOf(char c)Returns aCharacterinstance representing the specifiedcharvalue.
-
-
-
Field Detail
-
MAX_RADIX
public static final int MAX_RADIX
The maximum radix available for conversion to and from strings. The constant value of this field is the largest value permitted for the radix argument in radix-conversion methods such as thedigitmethod, theforDigitmethod, and thetoStringmethod of classInteger.
-
MAX_VALUE
public static final char MAX_VALUE
The constant value of this field is the largest value of typechar,'\uFFFF'.- See Also:
- Constant Field Values
-
MIN_RADIX
public static final int MIN_RADIX
The minimum radix available for conversion to and from strings. The constant value of this field is the smallest value permitted for the radix argument in radix-conversion methods such as thedigitmethod, theforDigitmethod, and thetoStringmethod of classInteger.
-
MIN_VALUE
public static final char MIN_VALUE
The constant value of this field is the smallest value of typechar,'\u0000'.- See Also:
- Constant Field Values
-
SIZE
public static final int SIZE
The number of bits used to represent acharvalue in unsigned binary form, constant16.- See Also:
- Constant Field Values
-
-
Method Detail
-
compare
public static int compare(char x, char y)Compares twocharvalues numerically. The value returned is identical to what would be returned by:Character.valueOf(x).compareTo(Character.valueOf(y))
- Parameters:
x- the firstcharto comparey- the secondcharto compare- Returns:
- the value
0ifx == y; a value less than0ifx < y; and a value greater than0ifx > y
-
digit
public static int digit(char ch, int radix)Returns the numeric value of the character ch in the specified radix.- Parameters:
ch- the character to be converted.radix- the radix.- Returns:
- the numeric value represented by the character in the specified radix.
-
forDigit
public static char forDigit(int digit, int radix)Determines the character representation for a specific digit in the specified radix. If the value ofradixis not a valid radix, or the value ofdigitis not a valid digit in the specified radix, the null character ('\u0000') is returned.The
radixargument is valid if it is greater than or equal toMIN_RADIXand less than or equal toMAX_RADIX. Thedigitargument is valid if0 <= digit < radix.If the digit is less than 10, then
'0' + digitis returned. Otherwise, the value'a' + digit - 10is returned.- Parameters:
digit- the number to convert to a character.radix- the radix.- Returns:
- the
charrepresentation of the specified digit in the specified radix. - See Also:
MIN_RADIX,MAX_RADIX,digit(char, int)
-
isDigit
public static boolean isDigit(char ch)
Determines if the specified character is a digit.Some Unicode character ranges that contain digits:
'\u0030'through'\u0039', ISO-LATIN-1 digits ('0'through'9')'\u0660'through'\u0669', Arabic-Indic digits'\u06F0'through'\u06F9', Extended Arabic-Indic digits'\u0966'through'\u096F', Devanagari digits'\uFF10'through'\uFF19', Fullwidth digits
- Parameters:
ch- the character to be tested.- Returns:
trueif the character is a digit;falseotherwise.- See Also:
digit(char, int),forDigit(int, int)
-
isISOControl
public static boolean isISOControl(char ch)
Determines if the specified character is an ISO control character. 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'.- Parameters:
ch- the character to be tested.- Returns:
trueif the character is an ISO control character;falseotherwise.- See Also:
isSpaceChar(char),isWhitespace(char)
-
isLowerCase
public static boolean isLowerCase(char ch)
Determines if the specified character is a lowercase character.A character is lowercase if its general category type, provided by
Character.getType(ch), isLOWERCASE_LETTER, or it has contributory property Other_Lowercase as defined by the Unicode Standard.The following are examples of lowercase characters:
a b c d e f g h i j k l m n o p q r s t u v w x y z '\u00DF' '\u00E0' '\u00E1' '\u00E2' '\u00E3' '\u00E4' '\u00E5' '\u00E6' '\u00E7' '\u00E8' '\u00E9' '\u00EA' '\u00EB' '\u00EC' '\u00ED' '\u00EE' '\u00EF' '\u00F0' '\u00F1' '\u00F2' '\u00F3' '\u00F4' '\u00F5' '\u00F6' '\u00F8' '\u00F9' '\u00FA' '\u00FB' '\u00FC' '\u00FD' '\u00FE' '\u00FF'
Many other Unicode characters are lowercase too.
- Parameters:
ch- the character to be tested.- Returns:
trueif the character is lowercase;falseotherwise.- See Also:
isLowerCase(char),toLowerCase(char)
-
isSpaceChar
public static boolean isSpaceChar(char ch)
Determines if the specified character is a Unicode space character. A character is considered to be a space character if and only if it is specified to be a space character by the Unicode Standard. This method returns true if the character's general category type is any of the following:SPACE_SEPARATORLINE_SEPARATORPARAGRAPH_SEPARATOR
- Parameters:
ch- the character to be tested.- Returns:
trueif the character is a space character;falseotherwise.- See Also:
isWhitespace(char)
-
isUpperCase
public static boolean isUpperCase(char ch)
Determines if the specified character is an uppercase character.A character is uppercase if its general category type, provided by
Character.getType(ch), isUPPERCASE_LETTER. or it has contributory property Other_Uppercase as defined by the Unicode Standard.The following are examples of uppercase characters:
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z '\u00C0' '\u00C1' '\u00C2' '\u00C3' '\u00C4' '\u00C5' '\u00C6' '\u00C7' '\u00C8' '\u00C9' '\u00CA' '\u00CB' '\u00CC' '\u00CD' '\u00CE' '\u00CF' '\u00D0' '\u00D1' '\u00D2' '\u00D3' '\u00D4' '\u00D5' '\u00D6' '\u00D8' '\u00D9' '\u00DA' '\u00DB' '\u00DC' '\u00DD' '\u00DE'
Many other Unicode characters are uppercase too.
- Parameters:
ch- the character to be tested.- Returns:
trueif the character is uppercase;falseotherwise.- See Also:
isLowerCase(char),toUpperCase(char)
-
isWhitespace
public static boolean isWhitespace(char ch)
Determines if the specified character 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, orPARAGRAPH_SEPARATOR) but is not also a non-breaking space ('\u00A0','\u2007','\u202F'). - It is
'\t', U+0009 HORIZONTAL TABULATION. - It is
'\n', U+000A LINE FEED. - It is
'\u000B', U+000B VERTICAL TABULATION. - It is
'\f', U+000C FORM FEED. - It is
'\r', U+000D CARRIAGE RETURN. - It is
'\u001C', U+001C FILE SEPARATOR. - It is
'\u001D', U+001D GROUP SEPARATOR. - It is
'\u001E', U+001E RECORD SEPARATOR. - It is
'\u001F', U+001F UNIT SEPARATOR.
- Parameters:
ch- the character to be tested.- Returns:
trueif the character is a Java whitespace character;falseotherwise.- See Also:
isSpaceChar(char)
- It is a Unicode space character (
-
toLowerCase
public static char toLowerCase(char ch)
Converts the character argument to lowercase using case mapping information from the UnicodeData file.Note that
Character.isLowerCase(Character.toLowerCase(ch))does not always returntruefor some ranges of characters, particularly those that are symbols or ideographs.In general,
String.toLowerCase()should be used to map characters to lowercase.Stringcase mapping methods have several benefits overCharactercase mapping methods.Stringcase mapping methods can perform locale-sensitive mappings, context-sensitive mappings, and 1:M character mappings, whereas theCharactercase mapping methods cannot.- Parameters:
ch- the character to be converted.- Returns:
- the lowercase equivalent of the character, if any; otherwise, the character itself.
- See Also:
isLowerCase(char),String.toLowerCase()
-
toString
public static String toString(char c)
Returns aStringobject representing the specifiedchar. The result is a string of length 1 consisting solely of the specifiedchar.- Parameters:
c- thecharto be converted- Returns:
- the string representation of the specified
char
-
toUpperCase
public static char toUpperCase(char ch)
Converts the character argument to uppercase using case mapping information from the UnicodeData file.Note that
Character.isUpperCase(Character.toUpperCase(ch))does not always returntruefor some ranges of characters, particularly those that are symbols or ideographs.In general,
String.toUpperCase()should be used to map characters to uppercase.Stringcase mapping methods have several benefits overCharactercase mapping methods.Stringcase mapping methods can perform locale-sensitive mappings, context-sensitive mappings, and 1:M character mappings, whereas theCharactercase mapping methods cannot.- Parameters:
ch- the character to be converted.- Returns:
- the uppercase equivalent of the character, if any; otherwise, the character itself.
- See Also:
isUpperCase(char),String.toUpperCase()
-
valueOf
public static Character valueOf(char c)
Returns aCharacterinstance representing the specifiedcharvalue. If a newCharacterinstance is not required, this method should generally be used in preference to the constructorCharacter(char), as this method is likely to yield significantly better space and time performance by caching frequently requested values. This method will always cache values in the range'\u0000'to'\u007F', inclusive, and may cache other values outside of this range.- Parameters:
c- a char value.- Returns:
- a
Characterinstance representingc.
-
charValue
public char charValue()
Returns the value of thisCharacterobject.- Returns:
- the primitive
charvalue represented by this object.
-
compareTo
public int compareTo(Character anotherCharacter)
Compares twoCharacterobjects numerically.- Specified by:
compareToin interfaceComparable<Character>- Parameters:
anotherCharacter- theCharacterto be compared.- Returns:
- the value
0if the argumentCharacteris equal to thisCharacter; a value less than0if thisCharacteris numerically less than theCharacterargument; and a value greater than0if thisCharacteris numerically greater than theCharacterargument (unsigned comparison). Note that this is strictly a numerical comparison; it is not locale-dependent.
-
equals
public boolean equals(@Nullable Object obj)
Compares this object against the specified object. The result istrueif and only if the argument is notnulland is aCharacterobject that represents the samecharvalue as this object.- Overrides:
equalsin classObject- Parameters:
obj- the object to compare with.- Returns:
trueif the objects are the same;falseotherwise.- See Also:
Object.hashCode(),HashMap
-
hashCode
public int hashCode()
Returns a hash code for thisCharacter; equal to the result of invokingcharValue().- Overrides:
hashCodein classObject- Returns:
- a hash code value for this
Character - See Also:
Object.equals(java.lang.Object),System.identityHashCode(java.lang.Object)
-
toString
public String toString()
Returns aStringobject representing thisCharacter's value. The result is a string of length 1 whose sole component is the primitivecharvalue represented by thisCharacterobject.
-
-