🔤 Strings & Text · Beginner

chars, Character & code points in Java

Character.isDigit/isLetter, toUpperCase, emoji as surrogate pairs.

🧩 The mysteryCount the characters in "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'); // 97
🔮 Predict it

One letter later

'x' is 120. What does this print?

char c = 'x';
c++;
System.out.println(c);
System.out.println('x' + 1);
  1. y y
  2. y 121
  3. 121 121
  4. 121 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
⚠️ The trap

'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';    // 7

Emoji 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.

🔮 Predict it

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);
  1. 2 2
  2. 3 2
  3. 3 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 the real world

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

  1. char + int gives an int
  2. Character.isDigit / isLetter / isWhitespace classify chars
  3. Emoji outside the BMP take 2 chars (a surrogate pair)
  4. codePointCount or codePoints() count real characters
🤯 Did you know?

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'));
  1. true false Q
  2. true true Q
  3. false false q
  4. 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);
  1. B B
  2. 66 66
  3. B 66
  4. 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.

Next: turning "42" into 42 — and what happens when a user types "4x2".