Skip to content
Tutorials Center
My learning

Topics

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.

Java
char c = 'k';
boolean letter = Character.isLetter(c);     // true
char up = Character.toUpperCase(c);         // 'K'

Try it

Java Classifying and converting

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.

Java Code points and surrogate pairs

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

Java
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

  • isDigit is Unicode-wide. It returns true for digits in other scripts, such as Arabic-Indic digits. For ASCII-only checks use c >= '0' && c <= '9'.
  • getNumericValue versus c - '0'. The subtraction is the idiom for ASCII digits. getNumericValue('a') is 10, and it returns -1 for characters without a numeric value.
  • digit(c, radix) returns -1 when c is not a valid digit in that radix; it is the safe building block for custom number parsing.
  • isWhitespace excludes 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() gives SS. Use string methods for natural language.
  • Chars are numbers. char + int is an int. (char) (c + 1) gives the next character, and c++ works directly on a char variable.
  • Boxing cache. Character.valueOf caches 0 to 127, so == on boxed characters is unreliable above that range; use equals.
  • Use codePoints() on strings when emoji or rare scripts may appear. length() and charAt() count code units.
  • Locale. The Character methods are locale-independent, unlike String.toUpperCase() which depends on the default locale (the famous Turkish dotless i).