Fundamentals 8 min read

Why Java Strings Can’t Exceed 65,534 Characters: JVM Limits Explained

Java’s String length is constrained by both the int‑based array storage allowing up to 2^31‑1 characters and the JVM class‑file constant‑pool limit of 65,534 bytes for literal strings, a nuance explained through source code, JVM specifications, and practical experiments.

Java Backend Technology
Java Backend Technology
Java Backend Technology
Why Java Strings Can’t Exceed 65,534 Characters: JVM Limits Explained

Preface

In Java, the String class stores characters in a char[] array, which means its length is ultimately limited by the maximum size of an array and the return type of String.length(), which is int.

String Storage

The int type in Java can represent values up to 2^31‑1 (2,147,483,647), so an array can theoretically hold up to about 4 GB of characters.

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

However, when a String is defined as a literal, the JVM stores it in the class file’s constant pool, which imposes a stricter limit.

JVM Constant‑Pool Limit

Each constant‑pool entry has a u2 index, a 2‑byte unsigned value, giving a maximum of 2^16‑1 = 65535 bytes. The actual usable length for a literal String is 65534 bytes because one byte is needed for the terminating instruction.

The effective range for a constant‑pool String is therefore 0‑65534 characters; exceeding this range at compile time causes an error, while runtime‑generated strings can be much larger.

Experiment

By constructing a String of exactly 65,534 characters in a loop and assigning it as a literal, the code compiles and runs successfully, confirming the compile‑time limit.

StringBuilder sb = new StringBuilder();
for (int i = 0; i < 65534; i++) {
    sb.append('a');
}
String s = sb.toString(); // literal of length 65534

Conclusion

Java Strings have two relevant limits:

Runtime limit: up to 2^31‑1 characters (≈4 GB) because of array and int constraints.

Compile‑time literal limit: 65,534 bytes (effectively 65,533 characters) due to the constant‑pool u2 index.

Understanding both limits helps avoid unexpected compilation errors when embedding very long literals.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

javaJVMCompilationStringConstant PoolLength Limit
Java Backend Technology
Written by

Java Backend Technology

Focus on Java-related technologies: SSM, Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading. Occasionally cover DevOps tools like Jenkins, Nexus, Docker, and ELK. Also share technical insights from time to time, committed to Java full-stack development!

0 followers
Reader feedback

How this landed with the community

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.