Fundamentals 8 min read

Understanding the Length Limits of Java String: JVM Specification and Practical Limits

Java's String type has both theoretical and practical length limits: while the underlying char[] array can hold up to 2^31‑1 characters (~4 GB) due to int indexing, the JVM constant‑pool restricts literal strings to a maximum of 65 534 characters, as explained with code examples and spec excerpts.

Java Captain
Java Captain
Java Captain
Understanding the Length Limits of Java String: JVM Specification and Practical Limits

Many developers wonder whether Java's String has a length limit; the author encountered this question in interviews and in a real project where a large file was Base64‑encoded into a string. This article examines the specifications and practical limits.

String stores its characters in a char[] array, so understanding array limits is the first step.

In Java, array indices are of type int . The maximum value of an int is 2^31‑1 (2,147,483,647), which means a char[] can theoretically hold up to about 2.1 billion characters, roughly 4 GB of data.

int[] arr1 = new int[10]; // define an array of length 10
int[] arr2 = {1,2,3,4,5}; // array length is 5

However, when a String literal is compiled, it is stored in the class file's constant pool. The JVM specification defines the constant‑pool entry for a String as CONSTANT_String , which references a CONSTANT_Utf8_info structure. The length field of this structure is a u2 (unsigned 2‑byte) value, limiting the stored UTF‑8 bytes to 2^16‑1 = 65535 . Because the JVM needs one extra byte for the terminating instruction, the effective maximum length for a literal string is 65534 characters.

To verify this limit, the author built a string of length 65,534 using a loop, confirmed the length with an online tool, and then assigned it as a literal in source code. The compilation succeeded, demonstrating that the compile‑time limit is indeed 65,534 characters.

In summary, at runtime a String can be as large as the maximum int value (≈4 GB), but when defined as a literal the JVM constant‑pool restricts it to 65,534 characters (effectively 65,534 due to the required terminating byte). Exceeding this limit causes a compile‑time error, while concatenated or dynamically created strings can exceed it.

The article concludes by inviting readers to like, comment, and share their thoughts.

JavaJVMString()programming fundamentalsConstant PoolLength Limit
Java Captain
Written by

Java Captain

Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.

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.