Understanding Java String Length Limits and Compilation Constraints
This article explains the theoretical and practical limits of Java String length, covering compile‑time restrictions, UTF‑8 encoding effects, constant‑pool constraints, runtime constructor limits, memory consumption, and JDK 9 optimizations, with illustrative code examples.
1. First, the length() method of String returns an int, so theoretically the length cannot exceed the maximum int value.
2. The compiler source code below restricts strings with length greater than or equal to 65535 from compiling.
private void checkStringConstant(DiagnosticPosition var1, Object var2) {
if (this.nerrs == 0 && var2 != null && var2 instanceof String && ((String)var2).length() >= 65535) {
this.log.error(var1, "limit.string", new Object[0]);
++this.nerrs;
}
}Java character literals use UTF‑8 encoding, which uses 1‑4 bytes per Unicode character; most Chinese characters require three bytes.
// 65534 ASCII characters, compilation succeeds
String s1 = "dd..d";
// 21845 Chinese "自" characters, compilation succeeds
String s2 = "自自...自";
// One ASCII 'd' plus 21845 Chinese "自" characters, compilation fails
String s3 = "d自自...自";For s1, each ASCII 'd' occupies one byte, so 65534 bytes are well within the limit, allowing compilation.
For s2, each Chinese character occupies three bytes, totaling 65535 bytes, and the string length is 21845, both within limits, so compilation succeeds.
For s3, the single ASCII 'd' plus 21845 Chinese characters occupy 65536 bytes, exceeding the storage limit, causing compilation to fail.
3. The JVM specification imposes limits on the constant pool. UTF‑8 encoded Unicode strings in the constant pool are represented by the CONSTANT_Utf8 type.
CONSTANT_Utf8_info {
u1 tag;
u2 length;
u1 bytes[length];
}The length field is a u2 (unsigned 16‑bit integer), so the maximum length is 2^16‑1 = 65535 bytes.
4. Runtime limits
The runtime limitation of String mainly appears in its constructor. Below is one of the String constructors.
public String(char value[], int offset, int count) {
...
}The count parameter represents the maximum length of the string. Since int’s maximum value is 2^31‑1, the theoretical maximum length of a String at runtime is 2^31‑1.
In practice, the actual maximum length depends on the JVM’s available memory. The largest possible string would occupy:
(2^31‑1)*16/8/1024/1024/1024 = 2 GBThus, in the worst case, a maximum‑size string would require about 2 GB of memory, and if the JVM cannot allocate that much, it will throw an error.
Note: Since JDK 9, String storage has been optimized; the underlying representation uses a byte array instead of a char array, allowing Latin‑1 strings to save half the memory.
Source: juejin.cn/post/7343883765540831283
Backend Exclusive Technical Group
Build a high‑quality technical community; developers, technical recruiters, and anyone willing to share job referrals are welcome to join and help each other improve.
Communicate civilly, focusing on technology exchange , job referrals , and industry discussion .
Advertisers are not allowed; do not trust private messages to avoid scams.
Selected Java Interview Questions
A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.