In Java, java.lang.Character does two jobs. First, it is the wrapper class for the primitive type char: a Character object holds exactly one char value, so characters can live in collections and be null. Second, it is a toolbox of static methods that test and convert single characters: isLetter(), isDigit(), isWhitespace(), toUpperCase(), digit(), and many more.
The class is final and lives in java.lang, so it needs no import. Its answers come from the Unicode standard, not just ASCII: Character.isLetter('\u03B1') is true for the Greek letter alpha. It also defines handy constants such as Character.MIN_RADIX (2), MAX_RADIX (36), MAX_CODE_POINT (0x10FFFF), and SIZE (16 bits). The Character Methods index lists every guide.
Static tests, instance basics.
Almost every method is called on the class: Character.isDigit(c). Only a handful are called on a Character object: charValue(), equals(), compareTo(), hashCode(), and toString(). Create objects with valueOf() or autoboxing; the new Character(c) constructor is deprecated since Java 9.
Syntax
Java Syntax
Use a primitive char for single characters, and the static methods to test or convert them:
Java
char c = 'a'; // primitive, 16 bits
Character boxed = c; // autoboxing: same as Character.valueOf(c)
char back = boxed; // unboxing: same as boxed.charValue()
boolean isLetter = Character.isLetter(c); // true
char upper = Character.toUpperCase(c); // 'A'
int asNumber = c; // 97, a char is also a number
// Character x = new Character('a'); // deprecated since Java 9
Reference
char vs Character
They hold the same value, but behave differently:
Question
char
Character
What is it?
A primitive 16-bit unsigned number (0 to 65535)
An object that wraps one char
Can it be null?
No; the default is '\u0000'
Yes, and unboxing null throws NullPointerException
Autoboxing converts in both directions for you: assigning a char to a Character calls Character.valueOf(), and using a Character where a char is needed calls charValue().
Reference
chars, Code Points, and Surrogate Pairs
Unicode numbers every character with a code point from U+0000 to U+10FFFF. A Java char is one UTF-16 code unit and only reaches U+FFFF, so characters above it use two chars:
Example 1 — Count Letters, Digits, Spaces, and Others
Walk through a password-like string one char at a time and sort every character into a group with three static tests.
Java
public class CharacterHubClassify {
public static void main(String[] args) {
String text = "Room 42, Floor B!";
int letters = 0, digits = 0, spaces = 0, others = 0;
for (int i = 0; i < text.length(); i++) {
char c = text.charAt(i);
if (Character.isLetter(c)) {
letters++;
} else if (Character.isDigit(c)) {
digits++;
} else if (Character.isWhitespace(c)) {
spaces++;
} else {
others++;
}
}
System.out.println("letters=" + letters + " digits=" + digits
+ " spaces=" + spaces + " others=" + others);
}
}
Output
letters=10 digits=2 spaces=3 others=2
How It Works
One char at a time.charAt(i) hands each of the 17 characters of "Room 42, Floor B!" to the tests in turn.
Letters first.Character.isLetter is true for R, o, o, m, F, l, o, o, r, and B, which makes 10.
Then digits and spaces. The characters 4 and 2 pass isDigit, and the three blanks pass isWhitespace; the comma and ! fall into others.
Example 2 — char, Character, and Autoboxing in a List
Collections cannot hold primitives, so a List<Character> stores Character objects. Java boxes and unboxes for you.
Java
import java.util.ArrayList;
import java.util.List;
public class CharacterHubBoxing {
public static void main(String[] args) {
List<Character> capitals = new ArrayList<>();
for (char c : "New York City".toCharArray()) {
if (Character.isUpperCase(c)) {
capitals.add(c);
}
}
System.out.println("capitals: " + capitals);
Character first = capitals.get(0);
char raw = first;
System.out.println("first.equals('N'): " + first.equals('N'));
System.out.println("raw + 1 = " + (raw + 1));
}
}
Output
capitals: [N, Y, C]
first.equals('N'): true
raw + 1 = 79
How It Works
Boxing on add.capitals.add(c) receives a primitive char; Java wraps it in a Character object automatically, because a List only stores objects.
Printing the list. Each Character prints as its letter, so the three capitals of "New York City" appear as [N, Y, C].
Unboxing on assignment.char raw = first; copies the value out of the object. first.equals('N') is true, and raw + 1 is the number 79 because a char is promoted to int.
Example 3 — Count chars and Code Points in Text with an Emoji
The emoji U+1F600 needs two char values. Compare length() with a code point count, then walk the text one code point at a time.
Java
public class CharacterHubCodePoints {
public static void main(String[] args) {
String s = "Hi\uD83D\uDE00";
System.out.println("chars: " + s.length());
System.out.println("code points: " + s.codePointCount(0, s.length()));
int i = 0;
while (i < s.length()) {
int cp = s.codePointAt(i);
System.out.println("index " + i + ": " + cp + " uses " + Character.charCount(cp) + " char(s)");
i += Character.charCount(cp);
}
}
}
Output
chars: 4
code points: 3
index 0: 72 uses 1 char(s)
index 1: 105 uses 1 char(s)
index 2: 128512 uses 2 char(s)
How It Works
Four chars, three characters."Hi\uD83D\uDE00" holds H, i, and the two halves of the emoji, so length() says 4 while codePointCount says 3.
Read a whole code point. At index 2, codePointAt sees a high surrogate followed by a low surrogate and joins them into 128512, which is 0x1F600.
Step by the right amount.Character.charCount(cp) is 1 for H and i and 2 for the emoji, so the loop jumps from index 2 straight past the end instead of landing on the low surrogate.
java.lang.Character is the wrapper class for the primitive type char. A Character object holds one char value, and the class also provides many static methods to test and convert characters, such as isLetter, isDigit, and toUpperCase.
char is a primitive 16-bit value; it cannot be null and has no methods. Character is an object that wraps a char; it can be null and can be stored in collections such as List<Character>. Java converts between them automatically (autoboxing and unboxing).
No. The Character constructor has been deprecated since Java 9. Use Character.valueOf(c) or simply assign the char to a Character variable and let autoboxing do it.
String.length() counts char values (UTF-16 units). A character above U+FFFF, such as most emoji, takes two chars. Use codePointCount() to count real characters.
No. Character methods use the Unicode data built into the JDK and ignore the default locale. Locale rules, such as the Turkish dotless i, belong to String.toUpperCase(Locale).