Java String Max Size

Last Updated : 19 Oct 2026

The maximum size of a Java String depends on the implementation and the available memory. The length() method returns the number of UTF-16 code units in a string, and its return type is int.

In this chapter, you will learn about the maximum size of a Java string, how the length() method works, and how integer overflow affects calculations involving Integer.MAX_VALUE.

What is the Maximum Size of a String in Java?

In Java, a String represents a sequence of characters. A string is immutable, which means its contents cannot be changed after the object is created. For more information, see Immutable String in Java.

For example, consider the following string:

String str = "tpointtech";

The above string can also be created from a character array:

char[] ch = {'t', 'p', 'o', 'i', 'n', 't', 't', 'e', 'c', 'h'};
String str = new String(ch);

String length() Method in Java

The String class provides the length() method to determine the number of UTF-16 code units in a string.

Syntax

public int length()

The method returns an int representing the string's length. For more information, see Length of String in Java.

Java strings use UTF-16 internally. A character in the Basic Multilingual Plane generally occupies one char code unit, while a supplementary Unicode character is represented by a pair of char values: a high surrogate (\uD800–\uDBFF) and a low surrogate (\uDC00–\uDFFF). Therefore, length() does not always return the number of Unicode code points.

Since length() returns an int, Integer.MAX_VALUE (2,147,483,647) is the largest value it can return. However, this does not mean every Java implementation can create a string of that length. Actual string size is limited by JVM implementation details, available memory, and other resource constraints.

Example 1: Finding the Maximum String Size in Java

In the following example, we attempt to create character arrays of sizes close to Integer.MAX_VALUE, fill them with the character 'a', and convert them into strings. The program prints the length of each successfully created string or displays an error message when an allocation fails.

Java

import java.util.Arrays;

public class StringMaxSize {
    public static void main(String[] args) {
        for (int i = 0; i < 1000; i++) {
            try {
                char[] array = new char[Integer.MAX_VALUE - i];
                Arrays.fill(array, 'a');

                String str = new String(array);
                System.out.println(str.length());
            } catch (OutOfMemoryError e) {
                System.out.println(e.getMessage());
                System.out.println("Last: " + (Integer.MAX_VALUE - i));
                System.out.println("Last: " + i);
                break;
            }
        }
    }
}

Output:

Java String Max Size

<text color="secondary" size="sm">Note: The complete output is not shown because it can be very long. The actual result depends on the JVM implementation and available memory.</text>

In this example, the loop attempts to allocate arrays with sizes starting at Integer.MAX_VALUE and decreasing by one on each iteration. The Arrays.fill() method assigns the character 'a' to every array element. The String constructor then creates a string from the array, and length() returns its length.

Allocating an array of this size will usually fail on practical systems. The program catches OutOfMemoryError, which can occur when the JVM cannot allocate the requested memory. Catching this error does not guarantee that the application can safely continue.

Memory Requirements for Large Strings

Java strings and character arrays require memory, and the exact amount depends on the JVM implementation. In many modern JVMs, compact strings may use one byte per character for certain Latin-1 content or two bytes per UTF-16 code unit for other content.

String concatenation or resizing a StringBuilder may temporarily require additional memory for existing and newly allocated data. Therefore, the practical maximum string length is usually much smaller than the theoretical limit imposed by the int return type.

Example 2: Integer Overflow Beyond Integer.MAX_VALUE

Java's int type is a signed 32-bit integer, with a range from -2,147,483,648 to 2,147,483,647. Adding one to Integer.MAX_VALUE causes integer overflow, wrapping the result to Integer.MIN_VALUE.

Java

public class StringSizeBeyondLimit {
    public static void main(String[] args) {
        try {
            System.out.println(
                "Trying to initialize n with value Integer.MAX_VALUE + 1"
            );

            int n = Integer.MAX_VALUE + 1;
            System.out.println("n = " + n);
        } catch (Exception e) {
            System.out.println(e);
        }
    }
}

Output:

Trying to initialize n with value Integer.MAX_VALUE + 1
n = -2147483648

In this example, the expression Integer.MAX_VALUE + 1 overflows the int range and produces -2147483648. No exception is thrown because integer overflow for primitive integer arithmetic is defined behavior in Java.

This example demonstrates integer overflow; it does not mean that a string can grow beyond its implementation-specific maximum length.