Strings are the most widely used data types in programming. They are simple and practical, but understanding them in depth will bring us more benefits.
String cache pool:
To save memory and improve resource reuse, Java introduces the String cache pool concept.
Strings in the cache Pool cannot be recycled:
Strings in the cache pool will not be recycled by the garbage collection mechanism. They are basically resident memory. Therefore, if you consume too many string classes, memory overflow may occur (as described below ).
How to save it to the cache pool:
- Directly assign values to create objects
In Java, basic packaging types such as string, long, float, and Boolean can be directly used to create objects.
When a string is directly used to create an object, it first finds the object with the same string in the cache pool and points to the object in the cache pool. This avoids re-allocating memory for the object, in this way, the reuse is improved. Therefore, we recommend that you create objects by using multiple methods. This is also recommended in the previous blog.
- Call the constructor to create an object
This method calls the constructor. All new objects must be allocated with memory, so they do not point to the existing objects in the cache pool, in this way, the cache pool may contain multiple string objects with the same value.
Package biaodashi; /*** string about cache pool ** @ author CCF **/public class stringhuancunchi {/*** @ Param ARGs */public static void main (string [] ARGs) {// todo auto-generated method stub stringhuancunchi huancunchi = new stringhuancunchi (); huancunchi. test1 ();} public void test1 () {string str1 = "CCF"; string str2 = "CCF"; string str3 = new string ("CCC "); // new will have a new address allocation, so it will not point to the cache pool's "CCC" string str4 = New String ("CCC"); system. Out. println ("str1 = str2? "+ (Str1 = str2); system. Out. println (" str3 = str4? "+ (Str3 = str4);} running result str1 = str2? Truestr3 = str4? False
Result: str1 and str2 both point to the same object, and str3 and str4 are not the same object.
How can I point to an existing object in the cache pool:
To direct to the cache pool object, the object must be directly assigned a direct volume or multiple direct operation values, rather than calling methods or other variables, however, constants modified by final that can be replaced by macros can also be regarded as directly assigned values, because these values can be calculated by virtual machines when they are added to the class. So it can point to the cache pool.
Public void test1 () {string str1 = "CCF"; string str2 = "C" + "C" + "F"; // calculate the system using three direct values. out. println ("str1 = str2? "+ (Str1 = str2); Result: str1 = str2? True multiple direct connections can also be directed to the cache pool.
In the code above, string str2 = "C" + "C" + "F"
; Several objects are created in total ?????
Some may say N, but there is only one, because virtual machines directly compute them and do not need to create objects. This is also the benefit of using direct data.
Public void test1 () {string str1 = "CCF" + "Length:" + "3"; string str2 = "CCF" + "Length:" + str1.length (); system. out. println ("str1 = str2? "+ (Str1 = str2); // result: str1 = str2? False conclusion: str2 calls the str1.length () method. Therefore, the STR value cannot be calculated when the VM is added to the class. Therefore, the output is as follows.} public void test1 () {final string length1 = "3"; string length1 = "3"; string str1 = "CCF" + "Length:" + "3 "; string str2 = "CCF" + "Length:" + length1; // call the final variable string str3 = "CCF" + "Length:" + leng22; // call the common variable system. out. println ("str1 = str2? "+ (Str1 = str2); system. Out. println (" str1 = str3? "+ (Str1 = str3); Result: str1 = str2? Truestr1 = str3? False: The final-modified length1 has the same effect as the direct quantity. When the virtual machine is added, length1 directly replaces "3", and then it has nothing to do with length1, the effect can be said to be the same as that in str1. This is also the difference between final variables and other variables, called macro replacement.
Immutable string:
String
} A class is an immutable class. After an object is created, the object cannot be changed, but the daily code often operates on the string. Does this mean that the string is immutable? Take a look at this Code:
Public void teststring () {string str1 = "hello"; int oldid = system. identityhashcode (str1); str1 = str1 + "CCF! "; // Int newid = system. identityhashcode (str1); system. out. println ("oldid =" + oldid + "\ nnewid =" + newid);} result: oldid = 854453928 newid = 584020407 where system. identityhashcode (str1); the function is to print out the hashcode of the object. Each object has a unique hashcode, which is the unique identifier of the object. Obviously, the first and second hashcodes are different,
Come to some conclusions: After the string object is operated, it returns a new object. The previous object has not changed, but it changes the object referred to by the reference of Str, at this time, the object is a new object. However, the object was discarded before, but it has a cache pool, so it will not be reclaimed by the garbage collection mechanism, so there will be a memory leak, therefore, do not use a string if possible. However, when stringbuffer and stringbuilder perform string operations, the new object will not appear, and the same object will be referenced, which can reduce the disadvantages of string.
Public void teststringbuilder () {stringbuilder builder = new stringbuilder ("hello"); system. out. println ("ID =" + system. identityhashcode (builder); builder. append ("CCF! "); System. out. println ("ID =" + system. identityhashcode (builder); system. out. println (builder);} result: Id = 854453928id = 854453928 Hello CCF!