Nearby lessons

14 of 125

Java - Unicode System

📌 What You Will Learn
  • Understand why Java uses Unicode
  • Learn that a Java char is 2 bytes (16 bits)
  • Use unicode escape sequences \uXXXX

Learn how Java handles text from every language in the world using the Unicode system — a unique number for every character, stored in a 16-bit char.

What is Unicode?

Computers only understand numbers. To show text, every character must be given a unique number. Unicode is the international standard that assigns a unique number to every character of every language — English, Hindi, Chinese, Arabic, emojis and more.

In simple words: Unicode is one giant phone book for characters — every symbol in the world has its own number, so text never gets mixed up.

Java char is 16 bits

In C/C++ a char is 1 byte (8 bits) — enough for 256 characters, which is fine for English but not for the world's scripts. Java's char is 2 bytes (16 bits), able to store 65,536 different characters. That is why a Java program can print text in any language.

Example02
JCode Cell
1public class UnicodeDemo {
2 public static void main(String[] args) {
3 char letter = 'A';
4 System.out.println(letter); // A
5 System.out.println((int) letter); // 65 - the Unicode number
6 }
7}
Output
A
65

Unicode Escape Sequences (\uXXXX)

You can write any character by its Unicode number using the \uXXXX escape — XXXX is the 4-digit hexadecimal code:

Example03
JCode Cell
1public class UnicodeEscapes {
2 public static void main(String[] args) {
3 System.out.println("\u0041"); // A
4 System.out.println("\u20AC"); // Euro sign (currency)
5 System.out.println("\u0915"); // Devanagari letter ka
6 }
7}
Output
A
€
क

Key Points

  • char = 2 bytes = 16 bits in Java.
  • The first 128 codes match the old ASCII table, so English text looks the same.
  • \uXXXX writes a character by its hexadecimal Unicode number.
  • Unicode is why Java is called a language-independent platform — one program can show any language.
📝 Key Takeaways
  • Unicode gives every character in every language a unique number
  • A Java char stores 16 bits — enough for 65,536 characters
  • Write any character with the \uXXXX escape sequence