The string is the most frequently used class in Java, but it is not a primitive type, but rather a predefined string class to represent. As you can see from the source of the string class, string is implemented based on char[], and string in Java is an immutable string that cannot be changed after the string is initialized.
It is estimated that there is a question, the string can not be changed? The following two kinds are not all changes?
String a = "123";
A = "1234";
A = a + "F";
It should be noted here that the memory of the string is divided into two parts, the first memory heap in the contents of the specific content, that is, char[], followed by the memory stack is stored in the specific content of the address. The above-mentioned can not be changed for the part of the heap, and our usual string modification is to create a new char[in the heap, and then modify the stack to the address of the new char[], so the string changes in the C language compared to the content of the string in Java to modify the efficiency of a little bit less, At the expense of efficiency, the immutable strings can be shared.
We recommend that you use StringBuffer or StringBuilder instead of "+" in your daily development work, StringBuffer and StringBuilder create mutable strings and call ToString () after the modification is complete. You can turn it into an immutable string. StringBuffer and StringBuilder API are the same, the difference is that the former is thread-safe, the latter thread is not secure, and less thread synchronous processing in exchange for higher efficiency, Remember before the test seems to be StringBuilder efficiency is about three times times the StringBuffer, both of the speed is much higher than the "+" of the traditional stitching. Although the three ways of speed are processed at the millisecond level, there are still a few gaps in frequent calls, so it is strongly recommended to use StringBuffer and StringBuilder for string concatenation.
Another point is about the string comparison, the daily development of the string is often compared, the above mentioned in the memory of the string in the way, so there will be a case, the heap of two char[] content, but the location is not the same, using = = comparison found that the two do not want to wait, So string comparisons in Java need to use the Equals () method, which compares the contents of a string, regardless of where it is stored. The problem with the = = comparison is that there is an intermittent bug in the program, sometimes none, because two string objects point to the same char[] or two char[with the same content).
More information about string can be found in the API details.
Looking back, Java restudying (eight): string of Java Basics