Java StringBuffer and StringBuilder Differences (RPM) and some basic operation code of the String class

Source: Internet
Author: User

String literal constant
StringBuffer string variables (thread-safe) When multiple threads are accessed, no problem occurs (Synchronized)
StringBuilder string variables (non-thread safe) may cause problems when multiple threads are accessed

Briefly, the main performance difference between the string type and the StringBuffer type is that the 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 Strin G object, so it is best not to use a string to change the content, because each generation of the object will have an impact on the system performance, especially when there is no reference object in memory, the JVM's GC will start to work, the speed will be quite slow.
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. So in general we recommend the use of StringBuffer, especially if the string object is constantly changing. In some special cases, string object concatenation is actually interpreted by the JVM as a concatenation of StringBuffer objects, so the speed of a string object is not slower than the StringBuffer object, especially in the following string object generation, Stri NG efficiency is far faster than StringBuffer:
String S1 = "This was only a" + "simple" + "test";
StringBuffer Sb = new StringBuilder ("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, this is a JVM trick, in the JVM's eyes, this
String S1 = "This was only a" + "simple" + "test"; is actually:
String S1 = "This was only a simple test"; 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 was only a";
String S3 = "simple";
String S4 = "Test";
String S1 = S2 +s3 + S4;
At this point the JVM will behave in the same way as it did.
In most cases StringBuffer > String
StringBuffer
Java.lang.StringBuffer A variable sequence of characters for thread safety. A string-like buffer, but cannot be modified. Although it contains a specific sequence of characters at any point in time, some method calls can change the length and content of the sequence.
String buffers can be safely used with multiple threads. These methods can be synchronized if necessary, so all operations on any particular instance appear to occur in a serial order that is consistent with the sequence of method calls made by each thread involved.
The main operations on StringBuffer are the Append and insert methods, which can be overloaded to accept arbitrary types of data. Each method effectively converts the given data into a string, and then appends or inserts the character of the string into the string buffer. The Append method always adds these characters to the end of the buffer, while the Insert method adds characters at the specified point.
For example, if z refers to a string buffer object where the current content is "start", this method call Z.append ("le") causes the string buffer to contain "startle", and Z.insert (4, "le") will change the string buffer to contain " Starlet ".
in most cases StringBuilder > StringBuffer Java.lang.StringBuilde
Java.lang.StringBuilder A variable sequence of characters is 5.0 new. This class provides an API that is compatible with StringBuffer, but does not guarantee synchronization. This class is designed to be used as a simple replacement for stringbuffer, which is common when a string buffer is used by a single thread. If possible, it is recommended that this class be preferred because, in most implementations, it is faster than StringBuffer. The two methods are basically the same. (transferred from http://blog.csdn.net/rmn190/article/details/1492013)The String class operation code is as follows
Import Java.util.date;public class Test14 {public static void main (string[] args) {char[] CharA = {' A ', ' B ', ' C ', ' in '}; String stra = new string (CharA); System.out.println (Stra); SYSTEM.OUT.PRINTLN (1 + 2 + stra);//is directly connected to a string, plus is a string connector, is not connected to the string still arithmetic operator//output result is 3abcint len = Stra.length (); System.out.println (len);//length () method output is the number of characters, containing the Chinese character//4system.out.println (' d ' + "a");//characters directly connected to the string plus a string connector// DaSystem.out.println (' A ' + ' d ' + "a");//The character is not directly connected to the string before + is the arithmetic operation//197asystem.out.println (' a ' + ' d ');//The character is not connected to the string + is an arithmetic operation// 197SYSTEM.OUT.PRINTLN (1 + 2 + ' s ');//The character type is automatically converted to the int type of the encoded add//118SYSTEM.OUT.PRINTLN (Stra.indexof ("a"));//0 System.out.println (Stra.indexof ("D"));//Cannot find return-1 System.out.println (Stra.indexof (' a ')); search before//0 if character is automatically converted to int type Then match the characters in the string System.out.println (Stra.lastindexof ("a"));//0 to find System.out.println (Stra.lastindexof (""));// The empty string output is in fact the string length 4system.out.println (Stra.charat (3));//Returns the character System.out.println (Stra.substring (2)) of the specified index position;// Returns the specified index position after the string C in System.out.println (stra.substring (2,3));//Returns the specified index position to the final cableThe String cSystem.out.println (Stra.trim ()) that is cited (not included);//ignores the preceding and trailing spaces System.out.println (Stra.replace ("A", "a"); Replace System.out.println (Stra.startswith ("a"));//Pre-match trueSystem.out.println (Stra.endswith ("B"));//After matching falsestring StrB = new String ("abc"); String strc = new String ("abc"); String StrD = "abc"; String stre = "abc"; System.out.println (StrB = = STRC);//false New is re-creating "ABC" reference address in heap memory will be different System.out.println (StrB = = StrD);//false The StrD points to a string constant pool, strb to System.out.println (stre = = StrD), and//true to the same "ABC" System.out.println of the string constant pool ( Strb.equals (STRC))//true as long as the content is equal (in fact, the content is in a string constant pool)//contains a comparison equalsignorecase () that ignores the case; StrD = "D"; stre = "a"; String strf = "H";    System.out.println (Strd.compareto (stre));//3 positive D after a third position System.out.println (Strd.compareto (STRF));//-4 Positive D before H 4th position System.out.println (Strd.touppercase ());//d//Convert to lowercase tolowercase ();D ate Date = new Date (); System.out.println (String.Format ("%te", date));//The number of days to return time//format () method is a static method of string that needs to know the formatting parameters of a common type string strG = new String ("A, b c,d"); for (String str:strG.split (", |")) System.out.println (str);//String segmentation, Return string//split method parameter is a string, you can use the "|" Separate multiple separators//a b C dfor (String Str:strG.split (", |", 2)//method can limit the number of splits this is divided two times System.out.println (str);//result a B c,d two string array element/ Some basic operations of the/stringbuilder class StringBuilder Strbuilder = new StringBuilder ("Hello"); System.out.println (Strbuilder); Strbuilder.append ("World");//Add System.out.println (Strbuilder) at the end; Strbuilder.insert (5, "Java");//Specify the index location to add System.out.println (Strbuilder); Strbuilder.delete (5, 9);// Does not include the end index SYSTEM.OUT.PRINTLN (strbuilder);}}

  

Java StringBuffer and StringBuilder Differences (RPM) and some basic operation code of the String class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.