1, String uses char[] array to store the contents of the string. The subscript of the array is an integer (you can also refer to the construction method of String (char value [], int offset, int count), and you can know that the number of characters is expressed as an integer). Integer (32 bits specified in Java) represents the range of 2G, that is, the maximum length of Java array is 2G, that is, the string cannot exceed 2G.
Are there any other restrictions on the array size in 2.Java? In fact, the size of the array cannot exceed the size of the Java heap, and the maximum size of the Java heap can be specified by the startup parameter. If the Java heap is large enough, the maximum length of the array can be increased continuously.
3. So in theory, a string can't be more than 2G characters, and it is possible to be less than 2G characters.