String is one of the most important objects in software development. Generally, string objects or their equivalent objects (such as char arrays) occupy the largest space block in the memory. Therefore, how to efficiently process strings is the key to improving the overall system performance. 1. String objects and features String objects are important data types in Java, but they are not basic data types in Java. In C, the most common way to process strings is to use char arrays, but the disadvantage of this method is obvious. arrays themselves cannot encapsulate the basic methods required for string operations. In Java, String objects can be considered as extensions and further encapsulation of char arrays. In Java, the String class consists of three parts: char array, offset, and String length. The char array indicates the content of the String, which is the superset of the String represented by the String object. The real content of String needs to be located and intercepted in the char array by the offset and length. It is important to understand this. This will help you better understand the memory leakage problems caused by the String. subString () method described later. In Java, Java designers have made a lot of Optimizations to the String object, which is mainly manifested in the following three aspects. This is also the three basic features of the String object: immutability; immutability means that once a String object is generated, it cannot be changed. This feature of String can be generalized to the constant mode, that is, the state of an object does not change after the object is created. The main function of the constant mode is to omit the synchronization and lock wait time when an object needs to be shared by multiple threads and frequently accessed, thus greatly improving the system performance. Ps: the constant mode is a design mode that can improve the performance of multi-threaded programs and reduce the complexity of multi-threaded programs. Optimization for the constant pool: When two String objects have the same value, they only reference the same copy in the constant pool. When the same string appears repeatedly, this technology can greatly save memory space. String str1 = "abc"; String str2 = "abc"; String str3 = new String ("abc"); System. out. println (str1 = str2); // returns trueSystem. out. println (str1 = str3); // returns falseSystem. out. println (str1 = str3.intern (); // return true. The code above shows that str1 and str2 reference the same address, but str3 re-opens a memory space, the position of str3 in the constant pool is the same as that in str1. That is to say, although str3 occupies the heap space separately, it points to the same entity as str1. In the above sample code, the last line uses the intern () method, which returns the reference of the String object in the constant pool.