String objects are limited in length. String objects do not “store” strings of infinite length. Constraints on String length should be considered both at compile time and at run time.

1. Compile-time limitation: 65534

We know that string constants are put into the constant pool of the method area, which is restricted by the JVM specification. Each data item in the constant pool has its own type. Unicode strings encoded in UTF-8 in Java are represented as type CONSTANT_Utf8 in the constant pool.

CONSTANT_Utf8 has the following data structure:

CONSTANT_Utf8_info {
    u1 tag;
    u2 length;
    u1 bytes[length];
}
Copy the code

Let’s focus on the bytes array of length, which is where constant data is stored, and length is the maximum number of bytes the array can hold. Length is of type u2, u2 is an unsigned 16-bit integer, so the maximum length theoretically allowed is 2^16-1=65535. However, since the JVM requires 1 byte to represent the closing instruction, the maximum String length at compile time cannot exceed 65534 bytes.

Run time limit: 2^ 31-1

The contents of a string are stored by an array of characters, char[], whose length and index are int. In Java, the maximum length of an int is 2^ 31-1.

But this is also the theoretical length, and the actual length depends on the memory of your JVM. How much memory the largest string will take up is 4GB. The calculation method is as follows:

(2^31-1)*2*16/8/1024/1024/1024 = 4GB
Copy the code

So in the worst case, one of the largest strings takes up 4GB of memory. If your virtual machine is unable to allocate this much memory, an error will be reported.

The storage of strings has been optimized in JDK9. Instead of using char arrays to store strings, the underlying layer uses byte arrays. Latin1-character strings can save twice as much memory.

Refer to the article: www.cnblogs.com/54chensongx…