Course topics

By WebNest Studio Engineering Team

Java - Core Tutorial

Unicode System in Java

Older languages designed in the 1970s, such as C, generally used ASCII to represent characters — a 7-bit encoding covering only 128 characters, which is enough for basic English text but nothing else. Java was designed later, in the era of international software, so its designers chose Unicode as the character encoding from the very beginning.

Because of this, Java's char type is defined as a 16-bit unsigned value, not the 8-bit (or smaller) representation used in many older languages, and it represents a single Unicode code unit rather than a single ASCII byte.

Why it matters

Any program that handles names, addresses or messages will meet text beyond English letters. Java strings are Unicode from the start, but a char is not always a whole character, and that gap is where bugs in length checks, truncation and reversing strings come from.

Why 16-bit Unicode Matters

A 16-bit char can represent 65,536 distinct values, which is enough to cover the Basic Multilingual Plane of Unicode — including Latin letters, Greek, Cyrillic, Arabic, Devanagari, Chinese, Japanese, Korean characters, and many symbols. This lets a single Java String hold mixed-language text (for example, an English label right next to Japanese or Hindi text) without any special encoding tricks, which was a real limitation in older 8-bit character systems.

Unicode Escape Sequences

Any character can be written using its Unicode code point with the escape sequence \uXXXX, where XXXX is a four-digit hexadecimal value. For example, '\u0041' represents the character 'A', and '\u20B9' represents the Indian Rupee sign (₹). These escapes work directly inside char and String literals, and even, unusually, outside of literals in raw source code, since the compiler processes Unicode escapes before parsing any other syntax.

A Note on Supplementary Characters

Some rarer Unicode characters — mostly historic scripts and certain emoji — lie outside the Basic Multilingual Plane and require more than 16 bits to represent. Java handles these "supplementary characters" using a pair of two chars called a surrogate pair, which is why methods like codePointAt() exist alongside the older charAt() for correctly reading such characters.

Example

Printing characters using Unicode escape sequences

Java
public class UnicodeDemo {
    public static void main(String[] args) {
        char letter = '\u0041';       // Unicode for 'A'
        char rupee = '\u20B9';        // Indian Rupee sign
        char devanagariKa = '\u0915'; // Devanagari letter for 'ka'

        System.out.println(letter);
        System.out.println(rupee);
        System.out.println(devanagariKa);
        System.out.println((int) letter);
    }
}
Output
A
₹
क
65

Common Mistakes

  • Assuming Java char is 8-bit like a C char — Java char is always 16-bit and Unicode-based.
  • Forgetting that a small number of Unicode characters (supplementary characters) need two chars (a surrogate pair) to represent, which can break naive character-by-character processing.
  • Writing an invalid four-digit hex value after \u and being confused by the resulting compile error.
  • Assuming Unicode support means all fonts or terminals can display every character — the encoding is correct even if the display environment cannot render a particular glyph.

Exercise

Explore how characters map to numbers. Complete the program so it prints the code of the letter A, the character with code 66, and the character written with the Unicode escape for 0041.

Expected output: 65, B, A, each on its own line.

Java · starter code
public class UnicodeDemo {
    public static void main(String[] args) {
        char letter = 'A';
        // TODO: print the numeric code of letter
        // TODO: print the character whose code is 66
        // TODO: print the character written as the Unicode escape \u0041
    }
}
Show hints
  • Casting a char to int gives its code: (int) letter.
  • Casting an int to char goes the other way: (char) 66.
Show solution
Java · solution
public class UnicodeDemo {
    public static void main(String[] args) {
        char letter = 'A';
        System.out.println((int) letter);   // 65
        System.out.println((char) 66);      // B
        System.out.println('\u0041');       // A
    }
}

Quick quiz

1. How many bits is a Java char?

  • A. 16
  • B. 8
  • C. 7
  • D. 32
Show answer

A. A char is a 16-bit UTF-16 code unit.

2. What does (int) 'A' evaluate to?

  • A. 1
  • B. 65
  • C. 41
  • D. 97
Show answer

B. The code of upper-case A is 65. Lower-case a is 97.

3. A String contains a single emoji that lies outside the first 65,536 Unicode code points. What does length() return?

  • A. 1
  • B. 4
  • C. 2
  • D. 0
Show answer

C. length() counts 16-bit chars, and such a character is stored as two chars (a surrogate pair). codePointCount() would return 1.

Interview questions

Why does Java use Unicode instead of ASCII?

Show answer

ASCII defines only 128 characters, enough for basic English. Java was designed for networked, international software, so it adopted Unicode, which assigns a number to characters from virtually every writing system. That lets one Java program handle Hindi, Chinese, Arabic and English text with the same String type.

What is the difference between length() and codePointCount() on a String?

Show answer

length() returns the number of 16-bit char values. codePointCount(0, s.length()) returns the number of actual Unicode characters. They are equal for most text, but differ when the string contains characters stored as surrogate pairs, where one character takes two chars.

Key Points to Remember

  • Java char is a 16-bit value based on Unicode, not the 7-bit ASCII used by many older languages.
  • 16 bits covers the Basic Multilingual Plane, enabling most world scripts in a single String.
  • Unicode escape sequences use the form \uXXXX with four hexadecimal digits.
  • Rare supplementary characters need a surrogate pair of two chars, handled via codePointAt() rather than charAt().

Practice in the playground

Write and run your own code for this topic, or start from the exercise or an example. Runs plain Java 17. Frameworks such as Spring, and databases, are not available here. Your code for each topic is kept in this browser.

Run the examples

Change an input, predict the result, then compare it with the output. Explain why the result changes.

WebNest Studio

Written by

WebNest Studio Engineering Team

Practical lessons from developers working with Java, Python, React, databases and production systems.

Led by Vansh Duggal, Founder, WebNest Studio · Full-stack & software engineering