Fundamentals 8 min read

Understanding Java String Length Limits: JVM Specification, Constant‑Pool Constraints, and Practical Experiments

This article explains why Java strings have length limits, detailing the char[] storage, the int‑based array size ceiling, the JVM constant‑pool u2 restriction of 65535 bytes, and demonstrates a compile‑time experiment building a 65,534‑character literal to illustrate the practical impact.

Top Architect
Top Architect
Top Architect
Understanding Java String Length Limits: JVM Specification, Constant‑Pool Constraints, and Practical Experiments

Java strings are stored as a char[] array, so their maximum length is bounded by the array size, which is an int (2^31‑1, roughly 2 GB). However, when a string literal is compiled, the JVM places it in the constant pool, where the index fields are unsigned 2‑byte values (u2), limiting the constant‑pool entry to 65 535 bytes.

The String.length() method returns an int , and Java arrays can be created up to int[] arr1 = new int[10]; or int[] arr2 = {1,2,3,4,5}; , confirming the theoretical limit.

According to the JVM specification, each CONSTANT_String entry references a CONSTANT_Utf8_info whose length field is a u2, so the maximum literal size is 65 535 bytes. Because the class file also needs one byte for the terminating instruction, the effective compile‑time limit is 65 534 characters.

At runtime, strings can exceed this limit because they are stored on the heap, not in the constant pool, and are only constrained by the int maximum.

An experiment builds a 65 534‑character string using a for loop, verifies its length with an online counter, and then assigns it as a literal; the compilation succeeds, confirming the theoretical limit.

In summary, Java strings are limited to 2^31‑1 characters at runtime, but compile‑time literals are restricted to 65 534 characters due to the constant‑pool u2 index size.

JavaJVMString()programming fundamentalsConstant PoolLength Limit
Top Architect
Written by

Top Architect

Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.