Scrivio Interview library
InternalsIntermediate6 min read · updated 12 November 2026

Explain compact strings (JEP 254, JDK 9). How is a String stored, what is the coder field, and when can compact strings hurt?

Since JDK 9, a String stores its characters in a byte[] plus a one-byte coder field. If every character fits in Latin-1, the coder is LATIN1 and each character takes one byte; otherwise the coder is UTF16 and each character takes two bytes, as in the old char[] layout. Latin-1 text therefore uses half the memory it used to, operations dispatch on the coder and are often JIT intrinsics, and the cost appears in text that is mostly non-Latin-1, which gains nothing, and in mixed text that forces inflation to UTF-16 and extra branches.

Why this question is asked

Compact strings were one of the biggest memory improvements in Java's history, because strings are often a quarter or more of a typical heap. Interviewers ask about them to see whether we understand how String is stored today, why charAt is still fast, and when the saving does not apply. In this answer, we will recap the old layout, look at the new fields and how the coder is chosen, see how methods use it, examine where it hurts, and answer whether charAt is still constant time.

1A Quick Recap of the Old Layout

Until JDK 8, a String held a char[]. A Java char is a 16-bit UTF-16 code unit, so every character used two bytes, even plain ASCII text such as "OK" or "customer_id", where the first byte of each char is always zero. Studies of real heaps found that most strings are Latin-1, so about half of all string memory was those zero bytes.

JEP 254 changed the internal representation in JDK 9 without changing any public method. Code that used String through its API kept working; only code that reached into its private fields with reflection broke.

2How a String Is Stored Now

A String now has a byte[] value and a byte coder. The coder is 0 for LATIN1 and 1 for UTF16. When a String is created, the JDK checks whether every character is at most U+00FF, the end of Latin-1. If so, it stores one byte per character; if any character is above that, it stores the whole string in UTF-16, two bytes per character.

Java
// What the fields look like inside java.lang.String, simplified
private final byte[] value;
private final byte coder; // LATIN1 = 0, UTF16 = 1
static final boolean COMPACT_STRINGS; // false if started with -XX:-CompactStrings
public int length() {
return value.length >> coder; // bytes / 1 for Latin-1, bytes / 2 for UTF-16
}
"hello" // LATIN1, value is 5 bytes
"héllo" // LATIN1 too: e with acute accent is U+00E9, inside Latin-1, 5 bytes
"hello世" // UTF16: one Chinese character makes the whole string 12 bytes
Bytes used for the character data
StringCoderJDK 8 (char[])JDK 9+ (byte[])
"hello"LATIN110 bytes5 bytes
"h" + e-acute + "llo"LATIN110 bytes5 bytes
"hello" + one CJK charUTF1612 bytes12 bytes
10 CJK charsUTF1620 bytes20 bytes
Object headers and array headers come on top of these figures.

Notice the second example: accented Western European letters such as e with an acute accent are in Latin-1, so French, German or Spanish text usually stays compact. And notice the third: one character outside Latin-1 switches the whole string, not just that character, to two bytes.

3How Methods Use the Coder

Every String operation checks the coder and calls one of two implementations, a Latin-1 version and a UTF-16 version, in internal classes called StringLatin1 and StringUTF16. Operations between two strings check both coders: equals returns false straight away when the coders differ, because a Latin-1 string can never equal a string that needed UTF-16.

That branch looks like overhead, but many of these methods are intrinsics: the JIT replaces them with hand-written machine code using vector instructions, for example for equals, indexOf, compareTo and the compress and inflate conversions. Working on one byte per character also means twice as many characters per cache line and per vector register, so Latin-1 operations are often faster than the old char[] ones, not just smaller.

4When Compact Strings Hurt

Compact strings help only when text is Latin-1. A service whose content is mostly Chinese, Japanese, Korean, Arabic or Cyrillic stores nearly every string as UTF-16, so it gains no memory, and it still pays the coder checks.

Mixed workloads have their own cost. Concatenating a Latin-1 string with a UTF-16 one produces a UTF-16 result, so the Latin-1 part is inflated, copied byte by byte into two-byte form. A StringBuilder that starts Latin-1 inflates its whole buffer the first time a non-Latin-1 character arrives. A workload that repeatedly mixes ASCII keys with non-Latin text does this conversion often, and takes the slower mixed-coder paths in comparisons. In practice the effect is usually small, which is why the feature is on by default.

Java
String key = "title:"; // LATIN1
String value = "東京"; // Tokyo in Japanese, UTF16
String line = key + value; // UTF16: "title:" is inflated to 2 bytes per char
System.out.println(line.length()); // 8 chars, stored in 16 bytes

It can be turned off with -XX:-CompactStrings, which makes every String UTF-16 again. That rarely helps: even all-CJK services usually have plenty of Latin-1 strings in keys, ids, URLs and JSON field names. Measure before considering it.

5Follow-up questions

Does charAt still run in constant time with compact strings?

Yes. charAt checks the coder and then reads one byte at value[index] for Latin-1, or two bytes at positions 2 * index and 2 * index + 1 for UTF-16, which is plain array indexing either way. The coder is fixed per string, so there is no scanning. Like before JDK 9, charAt returns a UTF-16 code unit, not a full code point, for characters outside the Basic Multilingual Plane.

Why Latin-1 and not UTF-8 internally?

Because UTF-8 is variable length: a character can take 1 to 4 bytes, so charAt(i) would need to scan from the start, turning a constant-time operation into a linear one. Latin-1 and UTF-16 both have fixed widths for String's char-based API, so indexing stays O(1).

Did compact strings change the result of getBytes or length?

No. length() still counts UTF-16 code units and getBytes(UTF_8) still produces UTF-8. The change is entirely internal, which is exactly why it could ship to every application without code changes.

6Conclusion

In this answer, we saw that JDK 9 replaced String's char[] with a byte[] plus a coder, LATIN1 at one byte per character or UTF16 at two, chosen once when the string is created. Latin-1 text, which includes most Western European accented letters, uses half the memory, and operations dispatch on the coder, often through JIT intrinsics. The saving disappears for text that is mostly outside Latin-1, and mixing the two forces inflation and mixed-coder paths. charAt stays constant time, and -XX:-CompactStrings exists but is rarely worth using.

What to say in the interviewAbout 90 seconds aloud

Open with

Compact strings, JEP 254 in JDK 9, changed how String stores its characters. Before, it was a char array, so two bytes per character even for plain ASCII.

Then, if they want more

Now it is a byte array plus a coder byte. When the string is created, the JDK checks whether every character is in Latin-1, that is up to U+00FF. If so, the coder is LATIN1 and it stores one byte per character.

If even one character is outside Latin-1, the coder is UTF16 and the whole string uses two bytes per character, like before. Length is just the array length shifted by the coder.

Every method branches on the coder into a Latin-1 or a UTF-16 implementation, and the important ones like equals, indexOf and compareTo are JIT intrinsics, so Latin-1 work is often faster as well as half the size. Typical heaps are mostly Latin-1 strings, so this was a big saving for free. It hurts in two cases.

Text that is mostly non-Latin-1, like Chinese or Japanese content, stays two bytes per char, so there is no saving. And mixing Latin-1 and UTF-16 strings forces inflation, for example when concatenating them or when a builder gets its first non-Latin-1 character. It can be disabled with minus CompactStrings, but that is rarely useful.

And charAt is still constant time, because both encodings have a fixed width, so it is just array indexing.

About this answer

Last updated
12 November 2026
Versions it names
Java 9, Java 8
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Explain compact strings (JEP 254, JDK 9). How is a String stored, what is the coder...": https://scrivio-interview-library.netlify.app/java/strings/eaa1cd46c9/