Rotten Pen--java. String

Source: Internet
Author: User

People say sting+ is inefficient.

string+The compile steps are:NewStringBuilder ()Newstring.valueof () StringBuilder.<init>stringbuilder.append () stringbuilder.tostring () and StringBuilder's compile steps are: Stringbuilder.append () and StringBuffer () The compilation Steps are: Stringbuffer.append () More detailed description, reproduced as follows. Respect the knowledge and respect the original author. Original connection: http://blog.csdn.net/rmn190/article/details/1492013String string constant stringbuffer strings variable (thread safe) StringBuilder string variable (non-thread safe) briefly, the main performance difference between the string type and the StringBuffer type is actually 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 string object, so it is best not to use a string to change the content frequently. Because each generation of objects will have an impact on system performance, especially when there are no more references in memory, the JVM's GC will start to work, and 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=NewStringBuilder ("This is just a"). Append ("simple"). Append ("test"); you will be surprised to find that the speed of generating a String S1 object is simply too fast, and this time Stringbuff Er incredibly speed 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"; in fact, it is: String S1="This is just a simple test"; So of course it doesn't take much time. But what you should note here is 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, the JVM will behave in the same way as it did in most cases StringBuffer>StringStringBufferJava.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 calls Z.append ("Le") causes the string buffer to contain "startle", while Z.insert (4, "le"The string buffer is changed to include "starlet". In most cases StringBuilder>StringBufferjava.lang.StringBuildejava.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. 

Rotten Pen--java. String

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.