Course contentsShow
← Java
Lesson 2001 of 2,87040. File I/O: Character StreamsFree lesson

UTF-16 and UTF-32

Learn UTF-16 (Java's internal encoding) and UTF-32, their tradeoffs, and use cases.

UTF-16 and UTF-32

You'll learn how Java's internal encoding (UTF-16) and the fixed-width UTF-32 work, and when you'd use each.

You've already seen UTF-8's variable-length approach. Now let's explore two other Unicode encodings that Java works with differently.

What Is UTF-16?

UTF-16 is Java's internal character encoding. Every char in Java is a 16-bit UTF-16 code unit. For most common characters (those in the Basic Multilingual Plane, code points U+0000 to U+FFFF), UTF-16 uses exactly one 16-bit unit—just two bytes.

But here's the catch: characters outside that range (like emoji, ancient scripts, or rare symbols) need two 16-bit units called a surrogate pair. So UTF-16 is mostly fixed-width, but not always—it's variable-length for less common characters.

Think of it like this: UTF-16 is optimized for languages that use characters in the lower Unicode ranges. It's more space-efficient than UTF-32 for typical text, but less predictable than UTF-8 for international data.

What Is UTF-32?

UTF-32 uses a fixed 32 bits (4 bytes) for every character, no exceptions. Every Unicode code point maps directly to one 32-bit integer. No surrogates, no variable lengths.

The tradeoff: It's simple and predictable, but wastes space—most text uses only a fraction of Unicode's range, yet every character still costs 4 bytes.

When to Use Each

  • UTF-16: Java's internal default. You usually encounter it when calling certain Java APIs or working with Windows systems (which use UTF-16 internally).
  • UTF-32: Rare in practice. Useful for algorithms that need constant-time character indexing or when working with systems that mandate it.
  • In Java I/O: You typically read and write UTF-8, but process text internally as UTF-16 (Java strings).

Key Takeaway: UTF-16 is Java's internal encoding—efficient for common characters but variable-length for rare ones; UTF-32 is always fixed-width but space-inefficient, making it rare outside specialized use cases.