UTF-8: Variable-Length Encoding
What you'll learn: How UTF-8 efficiently encodes any Unicode character using 1 to 4 bytes, making it the internet's dominant text encoding.
The Efficiency Problem Unicode Solves
You now know Unicode assigns a unique code point to every character in every language. But how do we actually store those code points as bytes? If we used a fixed 4 bytes per character (since Unicode has over a million possible code points), a simple English text file would waste enormous space—"Hello" would consume 20 bytes instead of 5.
How UTF-8 Works
UTF-8 is a variable-length encoding that uses:
- 1 byte for ASCII characters (code points 0–127) — letters, digits, basic punctuation
- 2 bytes for characters in most European, Middle Eastern, and Indic scripts
- 3 bytes for most East Asian characters (Chinese, Japanese, Korean)
- 4 bytes for rare symbols, emoji, and ancient scripts
Think of it like smart luggage: A postcard needs a small envelope, a book needs a box, a piano needs a truck. UTF-8 uses exactly the space each character requires.
Why UTF-8 Dominates
- Backward compatibility: Pure ASCII text (English, code, HTML tags) uses just 1 byte per character—identical to old ASCII files
- Space efficiency: Common text uses far less space than fixed-width encodings
- Self-synchronizing: You can detect character boundaries even if you jump into the middle of a stream
When you create an InputStreamReader or OutputStreamWriter with StandardCharsets.UTF_8, Java handles this variable-length complexity for you—reading and writing complete characters regardless of their byte size.
Key Takeaway: UTF-8 uses 1–4 bytes per character based on the character's Unicode code point, balancing efficiency with universal support—which is why it's used in over 98% of web pages and is Java's recommended encoding.