What Is the Maximum Length of a String in Java? Theory vs. Practical Experiment
- Details
- Written by Nam Ha Minh
- Last Updated on 14 October 2026   |   Print Email
What is the maximum length of a String in Java?
Or, in other words:What is the largest String object you can create in Java?
I tried creating the largest possible String on my computer and got some clear numbers to share. However, I still ended up with a few mysterious results that are worth looking into more deeply.
The maximum number of characters in a String, as well as the maximum size of a String object, depends on how it is created - at compile time or runtime - the Java version being used, and the amount of memory available to the JVM.
1. The Theoretical Maximum Length of a String
If you have ever written Java code to get the length of a String, you probably used something like this:int size = myString.length();In Java, the maximum value of a signed int is approximately 2.14 billion.So, theoretically, a String object in Java can hold a maximum of roughly 2.14 billion characters, assuming that each character is represented by a single byte, such as with Latin-1 encoding for Western languages.
For Unicode text that requires two bytes per character, a Java String could hold roughly half of that number, or about 1.07 billion characters. The maximum number of characters can be less if a language requires more than 2 bytes to represent a single character.
2. The Maximum Length of a String Literal
However, the largest String literal you can declare directly in a Java class is far smaller than that number.It is only about 65 thousand characters. The reason is the class file's constant pool limit.
A string constant is stored using a CONSTANT_Utf8_info structure:
CONSTANT_Utf8_info {
u1 tag;
u2 length;
u1 bytes[length];
}u2 length;u2 means an unsigned 16-bit integer, so its largest possible value is: 65,535For ASCII characters, where each character requires one byte of storage, this suggests that the largest String literal that could theoretically be declared in a Java source file is 65,535 characters.
For example, this is the code of a simple Java program that declares a String literal containing the maximum number of characters, around 65.5K:
public class MaxString {
public static void main(String[] args) {
String maxString = "65 thousands characters here...";
System.out.printf("String length: %,d\n", maxString.length());
}
}If you put one extra character into the literal, the Java compiler reports this error:MaxString.java:3: error: constant string too long String maxString = "X0123456789:;<= ..."Did I write this program manually? Of course not.It would be extremely difficult to type and count the exact number of characters by hand, so I created another program instead:
package net.codejava;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
public class MaxStringGenerator {
public static void main(String[] args) throws Exception {
File file = new File("MaxString.java");
BufferedWriter writer = new BufferedWriter(new FileWriter(file));
writer.append("public class MaxString {\n");
writer.append("\tpublic static void main(String[] args) {\n");
writer.append("\t\tString maxString = \"");
int count = 0;
boolean stop = false;
while (!stop) {
for (char c = 48; c <= 90; c++) {
writer.append(c);
if (++count > 65_533) {
stop = true;
break;
}
}
}
writer.append("\";\n\n");
writer.append("\t\tSystem.out.printf(\"String length: %,d\", maxString.length());\n\n");
writer.append("\t}");
writer.append("\n}");
writer.flush();
writer.close();
}
}When you run MaxStringGenerator, it creates a MaxString.java source file that declares a String literal containing exactly 65,534 characters.And this is where things become interesting.Do you notice that the practical maximum is one character less than the theoretical value of 65,535?
This was the first mystery I encountered during the experiment.
What is that one byte reserved for?
The maximum String literal declared in Java
3. Creating the Largest String at Runtime
Now, if you create a String object at runtime, it can hold far more characters.In theory, it can go up to roughly 2.14 billion characters, depending on the amount of JVM heap memory available.
So I wrote another Java program that attempts to create the largest possible String object:
package net.codejava;
public class LargestString {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < Integer.MAX_VALUE - 2; i++) {
sb.append('A');
}
String s = sb.toString();
System.out.printf("String length: %,d", s.length());
}
}Again, I got another unexpected result.If I use the exact value of Integer.MAX_VALUE the program fails with this runtime error:java.lang.OutOfMemoryError: Requested array size exceeds VM limitThis still happens even if I increase the heap size, for example:-Xmx5gFive gigabytes of heap should be enough for the test I was performing.
So I had to reduce the requested size by two characters.
With that change, the program ran successfully and printed:
String length: 2,147,483,645So the largest runtime-created String I could create contained:
2,147,483,645 charactersThat is exactly: Integer.MAX_VALUE - 2This was the second mystery.
Why does the JVM effectively reserve two elements in such a large array?

Memory consumption when running largest String test
4. Why Does It Require About 4 GB of RAM?
Then another mystery appeared.Why does a String containing about 2.1 billion characters require roughly 4 GB of RAM, which is about double the memory footprint I expected?
If you look at the source code of the String class in Java 27, you can see that it uses a byte array for character storage:
private final byte[] value;We do not need rocket science or advanced mathematics here.A String containing approximately 2.1 billion characters should consume around 2.1 GB of memory if each character is stored using one byte.
Of course, the JVM needs additional memory for other objects, runtime structures, and the application itself.
Still, I would expect the total memory usage to remain below roughly 3 GB.
But in my experiment, it required about 4 GB. Why?

Verify the size of the all byte arrays in memory
5. Comparing It with Java 8
In Java 8 and earlier, a String was backed by an array of char:private final char value[];The primitive char type requires two bytes to represent a single character. That means the maximum memory footprint of a String object containing about 2.1 billion characters could reach roughly 4 GB under JDK 8.That number makes sense.
But here is the interesting part.
I ran my experiment using JDK 27, where compact strings are enabled by default and the tested characters can be stored using one byte each.
Yet the memory consumption was still roughly the same as what I would expect under JDK 8.
That was the third mystery raised by this experiment.
What I Found After Hours of Experimenting
After spending hours experimenting with String in Java, I could summarize my results as follows:- The maximum length of a String literal declared at compile time is 65,534 characters.
- The maximum length of a String object created at runtime is 2,147,483,645 characters.
- The largest String object in my experiment consumed approximately 4 GB of RAM.
The theoretical limits are easy to calculate.
The practical limits are where Java becomes more interesting.
About the Author:
Nam Ha Minh is certified Java programmer (SCJP and SCWCD). He began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.
Comments