A Java char looks like a letter, but inside it is just a number. 'A' is 65, 'B' is 66, and so on. Once you see that, many text tasks become easy: counting digits, changing case or building a simple cipher. This lesson covers the Character class and then looks at Unicode, the system that lets Java handle Bangla, Arabic, Chinese and even emoji. You will also see why length() does not always count what a person would call characters.
In this lesson you will learn
- How a
charis stored as a number, and char arithmetic - Testing characters with
isLetter,isDigitand friends - Building a Caesar cipher
- Unicode code points, Bangla text and Bengali digits
- Emoji, surrogate pairs and
codePointCount
You should know Java strings and loops.
Characters Are Numbers
public class Main {
public static void main(String[] args) {
char c = 'A';
System.out.println("c = " + c);
System.out.println("(int) c = " + (int) c);
System.out.println("c + 1 = " + (c + 1));
System.out.println("(char)(c+1) = " + (char) (c + 1));
System.out.println("'7' - '0' = " + ('7' - '0'));
String s = "Dhaka 1207, BD!";
int letters = 0, digits = 0, spaces = 0, other = 0;
for (char ch : s.toCharArray()) {
if (Character.isLetter(ch)) letters++;
else if (Character.isDigit(ch)) digits++;
else if (Character.isWhitespace(ch)) spaces++;
else other++;
}
System.out.println("letters=" + letters + " digits=" + digits
+ " spaces=" + spaces + " other=" + other);
System.out.println("toUpperCase('q') = " + Character.toUpperCase('q'));
System.out.println("isUpperCase('Q') = " + Character.isUpperCase('Q'));
System.out.println("getNumericValue('9') = " + Character.getNumericValue('9'));
// Caesar cipher: shift letters by 3, keep everything else
String msg = "Meet at 5pm";
StringBuilder out = new StringBuilder();
for (char ch : msg.toCharArray()) {
if (Character.isUpperCase(ch)) out.append((char) ('A' + (ch - 'A' + 3) % 26));
else if (Character.isLowerCase(ch)) out.append((char) ('a' + (ch - 'a' + 3) % 26));
else out.append(ch);
}
System.out.println("Caesar +3 = " + out);
}
}
Output
c = A
(int) c = 65
c + 1 = 66
(char)(c+1) = B
'7' - '0' = 7
letters=7 digits=4 spaces=2 other=2
toUpperCase('q') = Q
isUpperCase('Q') = true
getNumericValue('9') = 9
Caesar +3 = Phhw dw 5sp
- Casting a
chartointshows its number:'A'is 65. c + 1gives theint66, because Java turns thecharinto anintbefore adding. Cast back with(char)to get'B'.- The digits
'0'to'9'have codes in order, so'7' - '0'is the number 7. This is the classic way to turn a digit character into a value. - The counting loop uses
Character.isLetter,isDigitandisWhitespace. These are clearer than checking ranges by hand, and they also work for letters outside English. - The Caesar cipher moves each letter 3 places.
ch - 'A'gives a position from 0 to 25,% 26wraps Z back round to C, and adding'A'turns the position back into a letter.
| Method | Returns true for |
|---|---|
isLetter(c) | Letters in any language |
isDigit(c) | Digits in any script, including Bengali digits |
isLetterOrDigit(c) | Either of the above |
isWhitespace(c) | Space, tab, new line |
isUpperCase(c), isLowerCase(c) | Capital or small letters |
Helpful converters: toUpperCase(c), toLowerCase(c), and Character.digit(c, 10), which gives a digit’s value or −1 if it is not a digit.
Unicode, Bangla and Emoji
Unicode gives every character in every writing system its own number, called a code point. Code points are usually written in hexadecimal, such as U+09AC for the Bengali letter ba. Java stores text in UTF-16: most characters fit in one char, but some, like many emoji, need two.
public class Main {
static String fromCodePoints(int... cps) {
return new String(cps, 0, cps.length);
}
public static void main(String[] args) {
String bangla = fromCodePoints(0x09AC, 0x09BE, 0x0982, 0x09B2, 0x09BE); // the word "Bangla"
System.out.println("Bengali word length() = " + bangla.length());
System.out.print("Code points (hex) = ");
bangla.codePoints().forEach(cp -> System.out.print(Integer.toHexString(cp) + " "));
System.out.println();
System.out.println("isLetter(first) = " + Character.isLetter(bangla.charAt(0)));
System.out.println("Script of first char = " + Character.UnicodeScript.of(bangla.codePointAt(0)));
String digits = fromCodePoints(0x09E7, 0x09E8, 0x09E9); // Bengali digits 1, 2, 3
int value = 0;
for (char ch : digits.toCharArray()) value = value * 10 + Character.digit(ch, 10);
System.out.println("Bengali digits value = " + value);
String emoji = "Hi " + Character.toString(0x1F600); // "Hi " and a smiley face
System.out.println("Emoji text length() = " + emoji.length());
System.out.println("codePointCount = " + emoji.codePointCount(0, emoji.length()));
System.out.println("charAt(3) is a high surrogate = " + Character.isHighSurrogate(emoji.charAt(3)));
System.out.println("Smiley code point = U+" + Integer.toHexString(emoji.codePointAt(3)).toUpperCase());
System.out.println("Chars for U+1F600 = " + Character.charCount(0x1F600));
}
}
Output
Bengali word length() = 5
Code points (hex) = 9ac 9be 982 9b2 9be
isLetter(first) = true
Script of first char = BENGALI
Bengali digits value = 123
Emoji text length() = 5
codePointCount = 4
charAt(3) is a high surrogate = true
Smiley code point = U+1F600
Chars for U+1F600 = 2
- The program builds the word “Bangla” in Bengali script from five code points. We wrote them as numbers so the source file stays plain ASCII, but in your own code you can type Bangla text straight into a string.
length()is 5, but a reader sees fewer “letters”. Vowel signs such as া and marks such as ং are separate code points that join the letter before them.Character.UnicodeScript.oftells you which writing system a character belongs to: here,BENGALI.Character.digitunderstands Bengali digits ১২৩, so the loop turns them into the number 123.- The smiley U+1F600 is too big for one
char. UTF-16 stores it as a surrogate pair: twocharvalues. So"Hi " + smileyhaslength()5 but only 4 code points.
| Task | Safe for all text |
|---|---|
| Count characters | s.codePointCount(0, s.length()) |
| Loop over characters | s.codePoints().forEach(...) |
| Get the character at position i | s.codePointAt(i) |
| Turn a code point into a String | Character.toString(cp) |
For English text, charAt and length() work fine. For user names, chat messages or anything that might contain emoji, use the code point methods so you never cut a pair in half.
Common Mistakes
- Printing
c + 1and expecting a letter. The result is anint; cast it back tochar. - Using
'a' <= c && c <= 'z'to test for letters. It misses capitals and every non-English letter. UseCharacter.isLetter. - Trusting
length()as a character count. Emoji count as 2, and Bangla vowel signs count separately. - Cutting a string with
substringin the middle of an emoji. This leaves a broken half character. Find the position with code point methods first. - Mixing up
'7'and7. The character'7'has the code 55.
Practice Exercises
Exercise 1. Add up the digits in the string "Roll 2026-15".
Show answer
int sum = 0;
for (char c : "Roll 2026-15".toCharArray()) {
if (Character.isDigit(c)) sum += c - '0';
}
System.out.println(sum); // 2+0+2+6+1+5 = 16
Exercise 2. Write the decoder for the Caesar cipher above, so that "Phhw" becomes "Meet".
Show answer
Shift by 23 instead of 3, because 3 + 23 = 26 brings every letter back to where it started:
out.append((char) ('A' + (ch - 'A' + 23) % 26)); // and the same for 'a'
Frequently Asked Questions
How many bits is a char in Java?
A char is 16 bits and holds one UTF-16 code unit, a number from 0 to 65535. Characters above that range, such as most emoji, need two char values called a surrogate pair.
Can Java strings hold Bangla text?
Yes. Java strings are Unicode, so Bangla, Arabic, Hindi and other scripts work. Save your source files as UTF-8 and use code point methods when you need an exact character count.
Summary
- A
charis a number;'A'is 65 and'7' - '0'is 7. - Use
Character.isLetter,isDigitand friends instead of hand-written ranges. - Unicode gives every character a code point; Java stores text in UTF-16.
- Emoji need two
charvalues, solength()can be bigger than the number of characters. - Use
codePoints()andcodePointCountfor text from real users.
