The difference between string and StringBuilder in Java

Source: Internet
Author: User
Tags first string

This article describes the difference between string and StringBuilder in Java.

I believe that the difference between the String and StringBuffer has been very well understood, but there will be many comrades on the work of these two classes are not clear of the place, today I am here to re-put this concept for you to review, by the way J2SE 5.0 Inside bring a new character operation Class--stringbuilder (don't be busy throwing my bricks, I'm sober, I'm not talking about C #, Java has StringBuilder class). So what's the difference between this StringBuilder and StringBuffer and the first String class we met? Which one should we use on different occasions? I would like to talk about some of these kinds of views, but also hope that everyone put forward their views, where everyone is wrong, in the mistake of change at the same time is a good opportunity to learn.

Briefly, the main performance difference between the string type and the StringBuffer type is that the string is an immutable object (why?). Ask the Java designer, why is String not a native type? Therefore, each time the string type is changed, 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 string to change the content of the strings, because each generation of the object will have an impact on the system performance, Especially when there are no more reference objects in memory, the JVM's GC will start to work, and the speed will be quite slow. Here is an example that is not very appropriate:

String S1 = "abc";
for (int I = 0; I < 10000; I + +)//For simulator multiple calls
{
S1 + = "def";
S1 = "ABC";
}

If this is the case, after the for loop, if the in-memory objects are not cleaned out by the GC, there is a total of more than 20,000 in memory, a staggering number, and if this is a lot of people use the system, so the number is not much, so you must be careful when using.

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"; 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";
String S1 = S2 +s3 + S4;

At this time the JVM will behave in the original way to do, S1 object generation speed is not as fast as it was just now, we can have a test to verify.

Thus we get the first conclusion: in most cases StringBuffer > String

And what about StringBuilder with them? First of all, StringBuilder is a new class in the JDK5.0, which is the difference from StringBuffer to see the following introduction (source Javaworld):

Java.lang.StringBuffer A variable sequence of characters for thread safety. String-like buffer that resembles string, but cannot be modified. 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.

Each string buffer has a certain amount of capacity. As long as the string buffer contains a sequence of characters that does not exceed this capacity, there is no need to allocate a new array of internal buffers. If an internal buffer overflows, this capacity is automatically incremented. 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.

This is to say that everyone can understand the difference between them, then we do a general deduction:

In most cases StringBuilder > StringBuffer

Therefore, according to the theorem of the transfer of this inequality: In most cases StringBuilder > StringBuffer > String (the greater the number of operations, the more stable).

The difference between string and 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.