chars, Character & code points in Java
Character.isDigit/isLetter, toUpperCase, emoji as surrogate pairs.
"hi😀". You say 3. Java's length() says 4. Who's right? Surprisingly — both of you.A char is a number in disguise
A char is a 16-bit UTF-16 code unit — really a small number. 'A' is 65, 'a' is 97. Cast it to see the number: (int) 'a' is 97.
char c = 'A';
int code = c; // 65
System.out.println((int) 'a'); // 97One letter later
'x' is 120. What does this print?
char c = 'x';
c++;
System.out.println(c);
System.out.println('x' + 1);y yy 121121 121121 y
Show the answer
y, then 121. c++ keeps c a char. But 'x' + 1 is arithmetic, and arithmetic promotes char to int, so println(int) prints a number.
Why the difference?
Binary + on a char produces an int. ++ and += include a hidden cast back to the variable's type, so the variable stays a char. To get a char from arithmetic, cast: (char) ('A' + 1) is 'B'.
The Character toolbox
Character classifies and converts chars: isDigit, isLetter, isWhitespace, isUpperCase, and toUpperCase (which returns a char, so it prints as a letter). getNumericValue('7') turns the digit into the number 7.
Character.isDigit('7'); // true
Character.isLetter('7'); // false
Character.toUpperCase('q'); // 'Q'
Character.getNumericValue('7');// 7'7' is not 7
(int) '7' is 55 — the character's code, not its digit value. To turn a digit character into a number, use Character.getNumericValue(c) or c - '0'.
char d = '7';
int wrong = d; // 55
int right = d - '0'; // 7Emoji take two chars
Characters outside the Basic Multilingual Plane — most emoji — don't fit in 16 bits. UTF-16 stores them as two chars, a *surrogate pair*. length() counts chars; codePointCount (or codePoints()) counts real characters.
Counting a smile
What does this print?
String s = "a😀";
System.out.println(s.length());
int cp = s.codePointCount(0, s.length());
System.out.println(cp);2 23 23 3
Show the answer
3 and 2. The emoji is one code point stored as two chars, so length() reports 3 while there are only 2 real characters.
In real projects
Character limits ("max 20 characters") counted with length() punish users who type emoji. Worse, cutting text with substring can split a surrogate pair in half, producing a broken ? symbol. Use code points when counting or truncating user-visible text.
Key takeaways
- char + int gives an int
- Character.isDigit / isLetter / isWhitespace classify chars
- Emoji outside the BMP take 2 chars (a surrogate pair)
- codePointCount or codePoints() count real characters
When Java launched in 1996, every Unicode character fit in 16 bits. Unicode 2.0 broke that limit the same year, and Java 5 added the code point methods to cope with characters beyond the first 65,536.
Practice questions
What does this print?
System.out.println(Character.isDigit('7'));
System.out.println(Character.isLetter('7'));
System.out.println(Character.toUpperCase('q'));- true false Q
- true true Q
- false false q
- true false 81
Check your answer
true false Q. '7' is a digit, not a letter. toUpperCase(char) returns a char, so println prints the letter Q.
What does this print?
char c = 'A';
c++;
System.out.println(c);
System.out.println('A' + 1);- B B
- 66 66
- B 66
- 66 B
Check your answer
B 66. c++ includes an implicit cast back to char, so c is 'B'. 'A' + 1 is int arithmetic, so println receives 66.