Reprint Please specify the Source:Jiq ' s technical Blog
(1) string nature:
The string object is immutable, and any action that appears to modify a string object actually creates a completely new sting object and then modifies it on that new object. And the more efficient StringBuilder are just different.
(2) The difference between string and StringBuffer:
The main performance difference between the string type and the StringBuffer type is actually that string is an immutable object, so each time a change is made to the string type it is equivalent to generating a new string object and then pointing the pointer to the new string object.
If you use the StringBuffer class, the result will be different, and each result will operate on the StringBuffer object itself, instead of generating a new object and changing the object reference.
(3) The difference between StringBuffer and StringBuilder:
StringBuffer is a thread-safe version of StringBuilder. Starting with JDK 5.0, an equivalence class, StringBuilder, is added to this class for use by a single thread. The StringBuilder class should usually be preferred over this class because it supports all the same operations, but it is faster because it does not perform synchronization.
However, it is not safe to use an instance of StringBuilder for multiple threads. For such synchronization, it is recommended to use StringBuffer.
(4) Related categories:
String,stringbuilder,pattern + Matcher,scanner.
(5) Code:
ASCII encoding (IOS-8895-1), ASICII encoding localization (GB2312,BIG5, etc.), Unicode internationalization encoding (UTF-8, UTF-16, etc.).
(6) Two bytes:
Note that Char in Java, whether it's a kanji or not, is two bytes.
(7) Chinese character recognition:
/** * Java judges the current character (two bytes) by judging the range of Unicode codes of the current character is not a kanji * */staticvoid Findchinese (String str) {string newstr = ""; for (int j=0;j& Lt;str.length (); j + +) {newstr = str.substring (j,j+1);//"\\u" is used to identify Unicode codes, and the subsequent four-bit 16-digit is the corresponding Unicode-encoded if ( Pattern.matches ("[\u4e00-\u9fa5]", newstr)) {System.out.print (NEWSTR);}} System.out.print ("\ n");}
(8) Compile-time modification and runtime modification:
About compile-time and runtime modification strings (to determine the difference between string contents): Any action that modifies a string at run time creates a new string object, but not at compile time.
can be seen from the above code, B1 and the B3 are modified at compile time, and B2 is modified at run time, so it re-creates a string object without pointing to the original " 123 "
There are also differences in efficiency between compile-time and run-time:
String S1 = "This isonly a" + "simple" + "test";
StringBuffer Sb = Newstringbuilder ("This was only a"). Append ("simple"). Append ("test");
You'll be surprised to find that the speed at which the String S1 object is generated is simply too fast, and at this point the stringbuffer is not at all dominant at all.
In fact, it's a trick of the JVM, in the JVM's eyes, this String S1 = "The is just a" + "simple" + "test"; It is actually: String S1 = "This was only a simple test"; The string S1 has been modified at compile time, so of course it doesn't take much time.
But it is important to note that if your string is from another string object, the speed is not so fast, for example:
String S2 = "This isonly a";
String S3 = "simple";
String S4 = "Test";
String S1 = S2 +s3 +s4;
This is when the content of the S1 is modified at run time, and you will honestly recreate an object and point S1 to it.
Java Basics: Strings