JDK source code: String, StringBuffer, StringBuilder, stringbuilder
String is the most commonly used in work, but it is still difficult to use it in many cases. It is necessary to analyze it as a whole. If you have read Thinking in java and read the JDK source code, many things will become very clear. Now we will analyze the underlying implementation of String.
First, for constructor, the most commonly used constructor in my work is the new String (str) constructor, so pay attention to this one here. This constructor parses the passed String and puts it into an array. We set it to arrayOfChar. The String class defines the global variable offset (offset), count (size ), value (an array of the char type). At this time, the offset is set to 0, and the count is set to arrayOfChar. length and value are set to arrayOfChar. At this time, the entire String constructor completes initialization, and all three global variables set the initial value. When a String-type object calls a method in the class, in fact, it is the operation on these three variables, which will be involved in the following method analysis. In the String method, many methods return new String objects because the size of the String is fixed after initialization. For example, the methods SubString and concat are described below.
Analysis of common methods: 1. The simplest isEmpty, the bottom layer is also a very simple line of code, return this. count = 0, clear and clear, well completed the corresponding functions. Here, the count in the constructor is used.
2. charAt: Get the character of the specified subobject. As mentioned before, the constructor processes the String into an array. In this case, if you want to specify the subscript character, it is easy. You can directly use [n] in the array to obtain it, however, it adds an offset and previously implemented simple Exception Processing to prevent array overflow.
3. equals compares strings with specified objects, one of the most common methods in development. At the beginning of the method, a judgment is made. If the current object is the same as the specified object, true is returned directly. Here, the current object uses the this keyword. If not, then perform the following operations. If the specified object is not of the String type (determined by instanceof), false is directly returned. If the object is of the String type, the array value of the current object and the array value1 of the specified object are taken out, then compare the array one by one based on the offset. If the array is equal, true is returned. Otherwise, if there is any element, false is returned.
4. compareTo compares two strings alphabetically. Return negative number, 0, positive number. In fact, the underlying implementation method is similar to equals, and is also implemented using arrays. However, the logic is different. It gets the array elements of the same underlying object one by one, and then obtains the dictionary order for comparison. If the values are equal, continue. If the values are not equal, directly subtract and return, this is a positive or negative number. If all values are equal, 0 is returned.
5. startWith (String, int) is used to determine whether the String starts with a specified String. The analysis here includes two parameters because startWith (String), endsWith (String) all of them are implemented by calling this method, which further demonstrates the convenience, fun, and conciseness of code reusability. It still uses arrays to compare the String parameter with the current object array one by one. Finally, the result is obtained. However, the second int parameter specifies the subscript in the array of the current object for String comparison. StartWith (String) actually gives the int value 0 in startWith (String, int) by default, indicating that the comparison starts from the array subscript 0. EndsWith (String) is to compare the length of the specified character array from the array subscript to the length of the current object array. In this way, the final implementation of the method is implemented by startWith (String, int. This should also be used in the work, that is, the reusability of the code, and finally the simplicity of the Code. Although the Code is short (one line to two lines), it implements its functions perfectly and is clear. I personally think that the method contains can also be implemented using the startWith (String, int) method. JDK uses indexOf for implementation. However, using startWith requires a loop, and the efficiency will certainly be much lower. [Here, I just put it one by one. Considering the efficiency, the indexOf is definitely better ].
6. substring (int1, int2) intercepts the string. substring (int) is called before, but the second int2 is set to the length of the current object array. It indicates that the element from the subscript int1 to the subscript int2 is obtained. If int2 is missing, the default value is the array length. The final implementation uses a String constructor Stirng (int, int, char []), returns a New String object. In this constructor, The New String object is initialized, including offset, count, and value. In this way, a new String object that meets the requirements is returned.
7. In fact, there are also common methods such as concat and replace. However, the underlying layer of concat and replace are all operations on arrays. The above mentioned are some typical ones. I will not talk about them one by one, you can directly view the source code, which is easy to understand. However, both of them return brand new String objects, and some other methods are used, but their ideas are similar. There are many other common methods such as getChars (char [], int), replaceAll (String, String ),SplitAll of them call the methods of the relevant classes, so I won't talk about them here.
8. There is a static internal class CaseInsensitiveComparator in the String, which is better here. This class only contains one compare method. This method is a string, but the case is ignored. Use charAt to retrieve the corresponding array elements, and then Character. toUpperCase for conversion comparison. For security purposes, he also uses Character. toLowerCase is used for conversion and comparison. If the values are equal, the next comparison is performed. If the values are not equal, the difference is directly returned. Methods In this static internal class cannot be directly accessed in other classes, but must be accessed through the compareToIgnoreCase method of the external class. I personally think This is to prevent other classes from directly accessing methods in static classes. It is a bit similar to the proxy mode. However, I still have some questions. I will raise them in the following questions. This method is also used in the traversal tool, but it is slightly different in the traversal tool, where the method is to return the object of the internal class, and then use this object to access the methods in the internal class.
In general, I have understood so much about the String method and learned how to implement it at the underlying layer. After reading the underlying implementation, I learned a lot, but I also had a lot of questions. I would like to list the questions and ask IT friends to help me.
Doubt: 1. The implementation of the method signorecase is return (this = paramString). Why can this case be ignored? What is the specific implementation method?
2. Why should I use an internal class for the internal class I just mentioned? I directly set the internal class method to private and then call it in the corresponding method, which can also achieve the same effect, maybe I don't know about internal departments, but why? What are the benefits?
3. The hashCode method assigned a value to the global variable hash of the class, but where is it used? Just assign values? Will it be related to String =? What is the relationship?
The above is purely a personal understanding of JDK source code, welcome prawns pointed out wrong or supplement, at the same time for doubt part of the certainly give us some advice, you can comment or private letter, you can also send an email to my aaddi@sina.com, any good learning methods can be shared at any time.
All IT friends are welcome to trust me and exchange and learn from each other!
StringBuffer and StringBuilder are not complete ......