String&stringbuffer&stringbuilder in Java

Source: Internet
Author: User

1.

strings, string Constants

StringBuffer, string variable (thread safe)

StringBuilder, string variable (non-thread safe)

2.String and StringBuffer

The main difference between a string and a Stringbuffe is that a string is an immutable object, and each time a string is changed, it actually generates a new string object, so strings that change content often do not use string types. Because every time a new object is formed, the performance of the system is affected, especially if the unreferenced objects in memory are more, the JVM's GC will start to work, and the speed will be quite slow.

StringBuffer each operation is performed on its own, rather than forming a new object, and then changing the object's reference, it is generally recommended to use StringBuffer, especially if the string object changes frequently. And in some special cases, string concatenation is actually interpreted as a concatenation of StringBuffer objects, so the speed of a string object is not slower than stringbuffer, especially in the formation of the following string object. The efficiency of a string is faster than StringBuffer:

String S1 = ' This was only a ' + ' simple ' + ' test ';

StringBuffer buffer = new StringBuffer (' This was only a '). Append (' simple '). Append (' Test ');

Because in the JVM's eyes,string S1 = ' This is just a ' + ' simple ' + ' test ', which is actually string S1 = ' This was only a simple test ';

But if that's the case:
String s2 = ' This was only a ';

String s3 = ' simple ';

String s4 = ' test ';

String S1 = s2+s3+s4;

This time the JVM will do it the way it used to,

In most cases: stringbuffer>string

3.StringBuffer

Java.lang.StringBuffer thread-safe variable character sequence, a string-like buffer, but cannot be modified. Although it contains a specific sequence of characters at any point in time, a method call can change the length and content of the sequence.

A string buffer can be used safely for multiple threads. These methods can be synchronized when necessary, so all operations on any particular instance appear to occur in a serial order that is consistent with the execution order of the thread that called the method.

The main operations on StringBuffer are append and insert operations that overload these methods to accept arbitrary data types, each of which effectively converts the given data to a string and appends or inserts the string into the character buffer.

4.StringBuilder

Java.lang.StringBuilder thread non-secure variable string sequence, is 5.0 new. This class provides a StringBuffer-compatible API, but does not guarantee synchronization. This class is designed to be used as a simple replacement for StringBuffer, When a string buffer is used by a single thread, the class is preferred if possible, because in most cases StringBuilder is faster than StringBuffer, and the methods are basically the same.

5\\

String&stringbuffer&stringbuilder in Java

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.