The String class is the most frequently used class in Java, even for beginners, because the main method takes an array of strings (String[] args). How long can a String be for such a frequently used class? 100,000 characters? A million characters? Or is it infinite?

To figure out the maximum length of a String, you should first understand the internal implementation of the String class. In the String class, an array of characters is used to maintain a sequence of characters, declared as follows:

private final char value[];Copy the code

That is, the maximum length of a String depends on the maximum length of an array of characters. We know that we can specify the length of an array using byte, short, char, and int, but not long. That is, the maximum length of an array is the maximum length of an int, 0x7ffFFfff, or 2147483647 in decimal, or the maximum number of characters a String can hold.

Java.lang.String#length()

public int length() {
    return value.length;
}Copy the code

You can see that the length method returns an int, not a long, for this reason.

However, this maximum value can only be achieved in theory, in our practical use, in general, the maximum length obtained is smaller than the theoretical value. Let’s write a simple procedure to see.

/** * @author wupx * @date 2020/01/13 */ public class StringTest { public static void main(String[] args) { char[] c = new char[Integer.MAX_VALUE]; }}Copy the code

Running this program usually produces the following error:

Exception in thread "main" java.lang.OutOfMemoryError: Requested array size exceeds VM limit
    at test.StringTest.main(StringTest.java:9)Copy the code

The cause of this error is memory overflow, which means the system cannot allocate such a large amount of memory. One char takes up 2 bytes, so 2,147,483,647 is 4,294,967,294 bytes. That’s close to 4GB, so it’s not surprising that attempts to claim such a large contiguous chunk of memory will fail.

So, how big a character array our computer can handle depends on many factors, such as software and hardware. We can write programs to get an approximation of the maximum number of characters that can be applied.

/** * @author wupx * @date 2020/01/13 */ public class StringTest { public static void main(String[] args) { for (int i = 0; i < 100; i++) { int len = Integer.MAX_VALUE - i; try { char[] ch = new char[len]; System.out.println("len: " + len + " OK"); } catch (Error e) { System.out.println("len: " + len + " " + e); }}}}Copy the code

The running results are as follows:

len: 2147483647 java.lang.OutOfMemoryError: Requested array size exceeds VM limit
len: 2147483646 java.lang.OutOfMemoryError: Requested array size exceeds VM limit
len: 2147483645 OK
len: 2147483644 OK
len: 2147483643 OKCopy the code

The maximum length of a String is integer. MAX_VALUE – 2 or 2 ^ 31-3.

conclusion

Inside the String class, a character array (char[]) is used to maintain character sequences.

The maximum length of a String is the maximum length of a character array. In theory, the maximum length of an int is 2147483647.

In practice, the maximum available is less than the theoretical maximum, and on my computer it’s 2 ^ 31-3, so you can test it on your computer.