Package java.lang

Class Character

  • All Implemented Interfaces:
    Serializable, Comparable<Character>

    public final class Character
    extends Object
    implements Serializable, Comparable<Character>
    The Character class wraps a value of the primitive type char in an object. An object of type Character contains a single field whose type is char.

    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 int MAX_RADIX
      The maximum radix available for conversion to and from strings.
      static char MAX_VALUE
      The constant value of this field is the largest value of type char, '\uFFFF'.
      static int MIN_RADIX
      The minimum radix available for conversion to and from strings.
      static char MIN_VALUE
      The constant value of this field is the smallest value of type char, '\u0000'.
      static int SIZE
      The number of bits used to represent a char value in unsigned binary form, constant 16.
    • Constructor Summary

      Constructors 
      Constructor Description
      Character​(char value)
      Constructs a newly allocated Character object that represents the specified char value.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      char charValue()
      Returns the value of this Character object.
      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.
      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.
      int hashCode()
      Returns a hash code for this Character; equal to the result of invoking charValue().
      static boolean isDigit​(char ch)
      Determines if the specified character is a digit.
      static boolean isISOControl​(char ch)
      Determines if the specified character is an ISO control character.
      static boolean isLowerCase​(char ch)
      Determines if the specified character is a lowercase character.
      static boolean isSpaceChar​(char ch)
      Determines if the specified character is a Unicode space character.
      static boolean isUpperCase​(char ch)
      Determines if the specified character is an uppercase character.
      static boolean isWhitespace​(char ch)
      Determines if the specified character is white space according to Java.
      static char toLowerCase​(char ch)
      Converts the character 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 toUpperCase​(char ch)
      Converts the character 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.
    • Constructor Detail

      • Character

        public Character​(char value)
        Constructs a newly allocated Character object that represents the specified char value.
        Parameters:
        value - the value to be represented by the Character object.
    • Method Detail

      • compare

        public static int compare​(char x,
                                  char y)
        Compares two char values numerically. The value returned is identical to what would be returned by:
         Character.valueOf(x).compareTo(Character.valueOf(y))
         
        Parameters:
        x - the first char to compare
        y - the second char to compare
        Returns:
        the value 0 if x == y; a value less than 0 if x < y; and a value greater than 0 if x > 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 of radix is not a valid radix, or the value of digit is not a valid digit in the specified radix, the null character ('\u0000') is returned.

        The radix argument is valid if it is greater than or equal to MIN_RADIX and less than or equal to MAX_RADIX. The digit argument is valid if 0 <= digit < radix.

        If the digit is less than 10, then '0' + digit is returned. Otherwise, the value 'a' + digit - 10 is returned.

        Parameters:
        digit - the number to convert to a character.
        radix - the radix.
        Returns:
        the char representation 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
        Many other character ranges contain digits as well.
        Parameters:
        ch - the character to be tested.
        Returns:
        true if the character is a digit; false otherwise.
        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:
        true if the character is an ISO control character; false otherwise.
        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), is LOWERCASE_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:
        true if the character is lowercase; false otherwise.
        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_SEPARATOR
        • LINE_SEPARATOR
        • PARAGRAPH_SEPARATOR
        Parameters:
        ch - the character to be tested.
        Returns:
        true if the character is a space character; false otherwise.
        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), is UPPERCASE_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:
        true if the character is uppercase; false otherwise.
        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, or PARAGRAPH_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:
        true if the character is a Java whitespace character; false otherwise.
        See Also:
        isSpaceChar(char)
      • 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 return true for some ranges of characters, particularly those that are symbols or ideographs.

        In general, String.toLowerCase() should be used to map characters to lowercase. String case mapping methods have several benefits over Character case mapping methods. String case mapping methods can perform locale-sensitive mappings, context-sensitive mappings, and 1:M character mappings, whereas the Character case 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 a String object representing the specified char. The result is a string of length 1 consisting solely of the specified char.
        Parameters:
        c - the char to 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 return true for some ranges of characters, particularly those that are symbols or ideographs.

        In general, String.toUpperCase() should be used to map characters to uppercase. String case mapping methods have several benefits over Character case mapping methods. String case mapping methods can perform locale-sensitive mappings, context-sensitive mappings, and 1:M character mappings, whereas the Character case 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 a Character instance representing the specified char value. If a new Character instance is not required, this method should generally be used in preference to the constructor Character(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 Character instance representing c.
      • charValue

        public char charValue()
        Returns the value of this Character object.
        Returns:
        the primitive char value represented by this object.
      • compareTo

        public int compareTo​(Character anotherCharacter)
        Compares two Character objects numerically.
        Specified by:
        compareTo in interface Comparable<Character>
        Parameters:
        anotherCharacter - the Character to be compared.
        Returns:
        the value 0 if the argument Character is equal to this Character; a value less than 0 if this Character is numerically less than the Character argument; and a value greater than 0 if this Character is numerically greater than the Character argument (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 is true if and only if the argument is not null and is a Character object that represents the same char value as this object.
        Overrides:
        equals in class Object
        Parameters:
        obj - the object to compare with.
        Returns:
        true if the objects are the same; false otherwise.
        See Also:
        Object.hashCode(), HashMap
      • toString

        public String toString()
        Returns a String object representing this Character's value. The result is a string of length 1 whose sole component is the primitive char value represented by this Character object.
        Overrides:
        toString in class Object
        Returns:
        a string representation of this object.