Lesson · Strings & Text
String internals: byte[] since 9, and cached hashes
10 min readString Internals
What a String object actually holds after JDK 9 compacted it from char[] to byte[] with a coder flag, plus the cached hash that makes strings good map keys.
From char[] to byte[] with a coder
Before JDK 9 a String held a char[] where every character cost two bytes, even pure ASCII. JEP 254 changed the representation to byte[] plus a final coder flag: Latin-1 strings store one byte per character, anything wider stores two. Most real-world strings are mostly ASCII, so the change cut heap use substantially and is why javap on modern builds shows byte[] and byte coder rather than char[].
Length in characters is still length(), computed from the byte length and the coder (divide by two for the wide form). The representation is invisible in normal code; it becomes visible in heap dumps and memory arithmetic, where a million-character ASCII string now costs about a megabyte rather than two.
The cached hash
String overrides hashCode with the polynomial hash (s[0]*31^(n-1) + ... + s[n-1]) and caches the result in a non-final int field. The first call walks the characters; later calls return the stored value. This matters because HashMap calls hashCode on every lookup, and caching turns repeat hashing into a field read.
The hash is computed lazily, so construction stays cheap and only strings that are actually hashed pay for it. The cache is also why the field is not final: immutability is what makes a staleable cache safe, one more dividend of the design covered in the string-type topic.
The substring memory leak, and its fix
In Java 6 and earlier, substring returned a view sharing the original char array, so a five-character substring of a megabyte string kept the megabyte alive: the famous substring leak. JDK 7 changed substring to copy the characters it needs, trading an O(n) copy for O(n) of garbage instead of an invisible retention.
That trade is the lesson worth keeping: views are cheap and dangerous (retention you did not mean), copies are costlier and safe. Java's collections later chose views deliberately (subList, keySet), where the same trade-off applies in full.
What the internals buy you
Compact representation plus cached hash plus immutability is why String is a legitimate HashMap key and a fast one. It is also why no operation ever writes into a string: the byte array is private and effectively fixed, so sharing is free.
The practical residue: stop thinking of String as 'a char array' (it has not been one for years), and stop worrying about length()'s cost (a field read plus a branch at worst). The details matter for interviews and memory profiling, not for daily syntax; that is what the next topics on encoding make explicit.
javap says byte[], but the API says characters
String.length() counts characters (code units, to be precise) regardless of bytes stored. UTF-8 encoded to disk, non-ASCII text occupies more bytes than its length; in the compact form, ASCII text occupies exactly its length. Bytes and chars are two different ledgers.
Interview angles
What does a String hold internally since Java 9?+
A final byte[] plus a coder flag: Latin-1 when every character fits one byte, UTF-16 when any does not. The goal (JEP 254) was halving the footprint of the overwhelmingly common mostly-ASCII string.
Why does String cache its hashCode?+
Strings are the dominant HashMap key, so hashing happens constantly. The cached int makes repeated hashing a field read. Immutability is what makes the cache sound: the value can never change.
What was the substring leak?+
Pre-JDK 7, substring shared the parent's character array: a tiny substring kept a large string alive as long as it lived. Modern substring copies, trading a cheap object for freedom from invisible memory retention.