Learn Java
import com.learnjava.variablesdatatypes.UnicodeChar;

Unicode & the char Type in Java

@Concept

Java's char type holds exactly one 16-bit Unicode character — not one byte, unlike C's char — which is why Java can represent characters from far more of the world's scripts than a single-byte encoding ever could. Internally, a char is really just a 16-bit unsigned integer, and Java freely lets you treat it as one: adding 1 to a char moves to the next code point, and a char can be assigned directly from an int literal that fits in that range.

Because a 16-bit value can only reach about 65,536 distinct code points, and Unicode itself now defines far more characters than that, some characters outside the common range need a pair of char values — called a surrogate pair — to represent a single visual character. One everyday consequence is that String.length() can occasionally report a different count than the number of visual characters a person would actually see.

@HowItWorks
1

A char is 16 bits

Java's char always occupies 16 bits, holding one UTF-16 code unit — a deliberate design choice for broader script support than a single byte allows.

2

Chars behave like small integers

Arithmetic works directly on a char: 'A' + 1 evaluates to the int 66, and adding a char to an int promotes the whole expression to int.

3

Character literals versus escape codes

A char can be written as a quoted letter like 'A', or as a Unicode escape like '\u0041', which represents the exact same character.

4

Casting between char and int

(char) 66 converts the integer 66 back into the character 'B'; the conversion is entirely mechanical, just reinterpreting the same numeric value.

5

Surrogate pairs

Characters outside the Basic Multilingual Plane need two char values together to represent one visual character, which is why String.length() can occasionally disagree with the number of characters a person would count on screen.

@Example
public class CharDemo {
public static void main(String[] args) {
char letter = 'A';
int code = letter + 1;
char next = (char) code;
System.out.println("Letter: " + letter + ", code: " + (int) letter);
System.out.println("Next letter: " + next);
}
}
Output
Letter: A, code: 65
Next letter: B
1letter holds the character 'A', which Java stores internally as the 16-bit value 65.
2letter + 1 promotes letter to an int before adding, so code ends up holding the int 66, not a char.
3(char) code casts that int back into a char, turning 66 back into the visual character 'B'.
4The two println calls each run once, showing both the numeric code behind 'A' and the character that comes right after it.
@UseCases
Converting between a letter and its numeric position in the alphabet, like for a simple Caesar-cipher exercise
Iterating over the letters 'a' through 'z' using a char loop variable and arithmetic
Understanding why a lone surrogate half can't stand alone as a valid char
Explaining why String.length() occasionally disagrees with the number of visual characters displayed on screen

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in Java yourself — building beats watching.