String length analysis?

Analysis of String length?
Once the length of the String is defined, it becomes unchangeable. It seems that when the size is changed, a space is actually opened up at the bottom layer, and then the variable name is the same as before, which means that the current String replaces the previous String. It is not a change in the essential sense. The bottom layer of String is implemented with a final modified char type array. It is immutable. It is not a simple type, but does it have a size! How many characters does he have at most? Generally speaking, the domain count is used to record the number of object characters. Because the type of count is of type int, it is obvious that the maximum length of String is 2^31, which is 4G, which is
actually not the case. In the class file In the specification, the constant_utf8_info table uses a 16-bit unsigned integer to record the length of the string, which can represent up to 65536 bytes, and the java class file uses a variant utf-8 format to store characters, null The value is represented by two bytes, so only 65536-2 = 65534 bytes are left. It is also the reason for the variant utf-8. If the string contains non-ascii characters such as Chinese, then the number of characters in the double quotes will be less (a Chinese character occupies three bytes). If this number is exceeded, the compiler will report an error during compilation.
I think it can be understood in this way. The bottom layer of String is implemented by a char type array, and the maximum allowable character length of char is 65536. In String, null requires two characters, so the maximum character of String is 65534.

Guess you like

Origin blog.csdn.net/qq_45874107/article/details/112485119
Recommended