Java Reference ยท java.lang
The Character class
Reference for java.lang.Character: isDigit, isLetter, toUpperCase, getNumericValue, code points, surrogate pairs and Unicode behavior.
Character wraps the primitive char and, more importantly, collects static methods that classify and convert characters: is it a letter, a digit, whitespace, upper case? What is its numeric value? It also contains the code point helpers needed to work with Unicode characters outside the Basic Multilingual Plane.
A Java char is a 16-bit UTF-16 code unit, not always a full character. Emoji and many rare scripts need two chars (a surrogate pair). For those, use the int overloads of each method, which take a code point.
char c = 'k'; boolean letter = Character.isLetter(c); // true char up = Character.toUpperCase(c); // 'K'
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Syntax
public final class Character implements Comparable<Character>, java.io.Serializable
Most methods exist twice: with a char parameter and with an int codePoint parameter.
| Method | Description |
|---|---|
isLetter, isDigit, isLetterOrDigit, isAlphabetic |
Unicode-aware classification. |
isUpperCase, isLowerCase, isWhitespace, isSpaceChar |
Case and whitespace tests. |
toUpperCase, toLowerCase, toTitleCase |
Case conversion of a single character. |
getNumericValue(char), digit(char, int radix), forDigit(int, int) |
Convert between digit characters and numbers. |
isSurrogate, isHighSurrogate, charCount, toChars, toCodePoint |
Surrogate-pair helpers. |
getType(char), getName(int) |
Unicode general category and character name. |
valueOf(char), charValue(), compare(char, char), toString(char) |
Wrapper basics. |
MIN_VALUE, MAX_VALUE, BYTES |
'\u0000', '๏ฟฟ', 2. |
Returns: boolean, char, int or String as named. Throws: IllegalArgumentException when an int method such as toChars(int) or toString(int) receives an invalid code point.
Usage notes
isDigitis Unicode-wide. It returnstruefor digits in other scripts, such as Arabic-Indic digits. For ASCII-only checks usec >= '0' && c <= '9'.getNumericValueversusc - '0'. The subtraction is the idiom for ASCII digits.getNumericValue('a')is10, and it returns-1for characters without a numeric value.digit(c, radix)returns-1whencis not a valid digit in that radix; it is the safe building block for custom number parsing.isWhitespaceexcludes no-break space.ยis a space character (isSpaceChar) but not whitespace, which surprises people cleaning web text.- Case conversion is imperfect.
toUpperCase('ร')(sharp s) staysร; the string method"ร".toUpperCase()givesSS. Use string methods for natural language. - Chars are numbers.
char + intis anint.(char) (c + 1)gives the next character, andc++works directly on acharvariable. - Boxing cache.
Character.valueOfcaches 0 to 127, so==on boxed characters is unreliable above that range; useequals. - Use
codePoints()on strings when emoji or rare scripts may appear.length()andcharAt()count code units. - Locale. The
Charactermethods are locale-independent, unlikeString.toUpperCase()which depends on the default locale (the famous Turkish dotless i).