The StringBuffer and Stringbuidler of common Java classes (iii)

Source: Internet
Author: User

Objective

The previous article introduced to you the string class, which we often use a class, then this one to share is StringBuffer and Stringbuidler. Wait, I'll compare the difference between the three of them.

I. StringBuffer1.1, overview

  

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 ".

1.2. Construction method

   

What is Charsequence?

Charsequence is a readable sequence of char values. This interface provides uniform read-only access to many different kinds of char sequences . A char value represents a character in a Basic multilingual Plane (BMP) or surrogate.

    

1.3. Common methods

1) Append () method

    

2) Capacity () method

    

3) charAt (int index) method

4) Delete () method

    

5) indexOf (String str)

    

5) Insert () method

    

6) Length () method

7)Reverse () method

    

8)substring () method

    

9) toString () method

    

Second, StringBuilder2.1, overview

So let's take a look: Abstractstringbuilder this abstract class:

    

It implements some basic methods of the class.

  

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,

  Used When a string buffer is used by a single thread (this is common). 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 .

2.2. Construction method

  

2.3. Common methods

It's basically the same as the stringbuffer above.

Iii. the difference between String, StringBuffer, StringBuilder

The first thing to note is:

string literal constant
StringBuffer string variable (thread safe)
StringBuilder string variable (non-thread safe)

  in most cases the three are compared in terms of execution speed: StringBuilder > StringBuffer > String

Explain:

  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,

The pointer then points to the new string object, so it is best not to use string to change the contents of the strings , because each generation of the object will have an impact on the system performance, especially when there are no more references in memory, the JVM's GC will begin to work . That speed is bound to 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.

  

Why is it most of the situation?

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. In particular, in the following String object generation, string 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 at all. In fact, this is a JVM trick, in the JVM's eyes, this
String S1 = "This was only a" + "simple" + "test"; It 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";

In most cases, the speed of the StringBuilder is greater than StringBuffer:

Java.lang.StringBuilder a variable sequence of characters is 5.0 new . (Most of the time we're doing it on a single thread, so in most cases it's recommended to use StringBuilder instead of StringBuffer) This class provides a stringbuffer-compatible API,

However, synchronization is not guaranteed. This class is designed to be used as a simple replacement for StringBuffer, Used When a string buffer is used by a single thread (this is common). 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.

 

For a summary of the three uses:

1) If you want to manipulate a small amount of data with = String
2) operation of a large amount of data in a single-threaded operation string buffer = StringBuilder
3) Multi-threaded operation string buffers operation of large amounts of data = StringBuffer

  

The StringBuffer and Stringbuidler of common Java classes (iii)

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.