String, StringBuilder and StringBuffer for JDK source code analysis
preface
This article mainly introduces the relevant contents of string, StringBuilder and StringBuffer of JDK source code analysis, which are shared for your reference and learning. I won't say more below. Let's take a look at the detailed introduction together
Declaration of string class
The string class uses the final modifier to indicate that it cannot be inherited. At the same time, it also implements three interfaces. Implementing the serializable interface means that the string class can be serialized; The implementation of the comparable < T > interface mainly provides a CompareTo method for comparing string strings; It also implements the charsequence interface, which represents a readable sequence of char values (charbuffer, segment, string, StringBuffer and StringBuilder also implement the charsequence interface)
String description of main fields and properties
String partial method analysis
The string class provides a series of constructors, several of which are not recommended, as shown in the following figure:
Constructor
The following are implementations of two common constructors:
boolean equals(Object anObject)
The string class overrides the equals method to compare this string with the specified object. The result is true if and only if the parameter is not null and is a string object representing the same character sequence as this object.
int compareTo(String anotherString)
Compare the character sequences of two strings bit by bit. If a character is different, the difference between the Unicode values of the two characters of the bit is returned. If all bits are the same, the difference between the lengths of the two strings is calculated. If the two strings are the same, 0 is returned
The implementation of comparetoignorecase (string STR) method is similar to this. The case of characters is ignored during comparison. The implementation method is as follows:
native String intern()
When the intern method is called, if the pool already contains a string equal to this string object (determined by the equals (object) method), the string in the pool is returned. Otherwise, add the string object to the pool and return a reference to the string object.
All literal strings and string assignment constant expressions use the intern method for operation, for example: string STR1 = "123";
String memory location: constant pool or heap
String objects can be created directly by literal or constructor. What's the difference?
1. The string object created by literal or literal string splicing with "+" is stored in the constant pool. During actual creation, if it exists in the constant pool, it will directly return a reference. If it does not exist, it will create the string object
2. If you use the constructor to create a string object, you can directly create a string object in the heap
3. Call the intern method, and the object will be put into the constant pool if it returns (if it does not exist, it will be put into the constant pool, and if it exists, it will return a reference)
The following example illustrates the memory allocation of string objects:
Examples of string splicing:
The decompiled code can be seen at a glance:
The memory allocation is as follows:
String、StringBuffer、StringBuilder
Because the attribute value [] character array internally maintained by the string type for storing strings is decorated with final:
It indicates that the string object can be modified after assignment. Therefore, we believe that the string object is immutable once it is created. If frequent string splicing operations are encountered in the development process, if you use the contact provided by string or directly use "+" to splice strings, new strings will be frequently generated, which is inefficient. Java provides two other classes: StringBuffer and StringBuilder to solve this problem:
Take a look at the following code:
The following is the implementation of the StringBuilder class. Only part of the code analyzed is intercepted:
StringBuffer is the same as StringBuilder. The internally maintained value [] character array is variable. The only difference is that StringBuffer is thread safe. It synchronizes all methods. StringBuilder is thread unsafe. Therefore, when multi-threaded operations share string variables, StringBuffer is the first choice for string splicing. Otherwise, StringBuilder can be used, After all, thread synchronization also brings some consumption.
summary
The above is the whole content of this article. I hope the content of this article has a certain reference value for your study or work. If you have any questions, you can leave a message. Thank you for your support for programming tips.