Fundamentals 8 min read

What Is the True Maximum Length of a Java String?

This article explores the theoretical and practical limits of Java String length, analyzing JDK source code, integer ranges, JVM constant pool constraints, and runtime memory considerations to reveal why the maximum size is 2^31‑1 characters (≈4 GB) at runtime but only 65 534 characters at compile time.

Senior Brother's Insights
Senior Brother's Insights
Senior Brother's Insights
What Is the True Maximum Length of a Java String?

When encountering the question "What is the length limit of a String?" many may find it trivial, but a deeper investigation reveals a rich set of concepts including the String implementation, int range, JVM specifications, and compiler behavior.

String Source Code Analysis

Using JDK 8 as an example, the String.length() method returns the length of the internal char[] array. Since the return type is int, the maximum value is bounded by Integer.MAX_VALUE = 2^31‑1 = 2147483647. Each char occupies two bytes (UTF‑16), so the theoretical memory consumption is 2147483647 * 2 = 4294967294 bytes, roughly 4 GB.

2^31 - 1 = 2147483647
2147483647 * 2 = 4294967294 (bytes)
4294967294 / 1024 / 1024 / 1024 ≈ 4 GB

However, declaring a literal string of 100 000 characters triggers a compile‑time error "constant string too long" because the compiler imposes its own limit.

Compile‑time Constant‑Pool Limit

String literals are stored in the class constant pool as CONSTANT_Utf8_info entries. The JVM spec defines the length field of this structure as an unsigned 2‑byte value ( u2), giving a maximum of 2^16‑1 = 65535 bytes. Due to an early design bug, the effective limit is 65 534 bytes.

public class Pool {
    public static final int MAX_STRING_LENGTH = 0xFFFF; // 65535
}

Attempting to compile a literal longer than 65 534 characters fails, confirming the compile‑time ceiling.

Runtime Length Limit

At runtime, the limit is governed by the constructor public String(char[] value, int offset, int count), where count is the length. The same int range applies, yielding a maximum of 2^31‑1 characters, i.e., about 4 GB of memory, provided the JVM can allocate that much heap.

(2^31‑1) * 16 / 8 / 1024 / 1024 / 1024 = 4 GB

JDK 9 introduced a compact string representation using a byte[] for Latin‑1 characters, halving the memory usage for such strings.

Conclusion

In summary, the maximum length of a Java String is:

Compile‑time literal: ≤ 65 534 characters (due to constant‑pool u2 limit).

Runtime: ≤ 2^31‑1 characters, limited by available heap memory (≈ 4 GB for UTF‑16 strings).

Understanding these constraints helps avoid unexpected compilation errors and informs memory‑aware string handling.

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.

JavaJVMRuntimeStringMemoryCompile-timeLength Limit
Senior Brother's Insights
Written by

Senior Brother's Insights

A public account focused on workplace, career growth, team management, and self-improvement. The author is the writer of books including 'SpringBoot Technology Insider' and 'Drools 8 Rule Engine: Core Technology and Practice'.

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.