The difference between String, StringBuffer and StringBuilder

Source: Internet
Author: User
Tags generator stringbuffer

If you want to learn Java software programming more or less know the difference between them, today to sum up:

String: Fixed-length strings, and a new string instance must be regenerated if the value has changed;

StringBuffer: When you use the Append and insert methods to stitch strings, each result operates on the StringBuffer object itself and does not generate new objects like string and then change the object reference. In addition StringBuffer is a thread-safe variable character sequence. A string buffer similar to strings, but cannot be modified. Although it contains a particular sequence of characters at any point in time, some method calls can change the length and content of the sequence. You can safely use string buffers with multiple threads. These methods can be synchronized as necessary, so all operations on any particular instance occur as if they were in a sequential order, in the same order as the method calls of each of the involved threads. Each string buffer has a certain capacity. When an operation related to a source sequence occurs, such as an append or insert operation in a source sequence, the class implements synchronization only on the string buffer that executes the operation, not on the source. You do not need to allocate a new internal buffer array as long as the string buffer contains a sequence of characters that does not exceed this capacity. This capacity is automatically increased if the internal buffer overflows.

The difference between string and StringBuffer:

To sum up, we can clearly find that the use of StringBuffer is recommended in cases where string objects often change, and that StringBuffer is more memory-saving than string. In this case, it seems that we have come to the difference between string and StringBuffer, hehe, in fact we have overlooked a point: in some special cases, string concatenation of strings is actually interpreted by the JVM as a concatenation of StringBuffer objects, So these times the string object's speed is not slower than the StringBuffer object, and in particular the following string object generation, string efficiency is far faster than StringBuffer:

String name1 = "Gao" + "Huan" + "Jie";
StringBuffer name2 = new StringBuilder ("Gao"). Append ("Huan"). Append ("Jie");

Running through a program you may be surprised to find that the Name1 object is generated faster than the speed block that generated the Name2 object. In fact, this is the JVM's ghost: In the eyes of the JVM, this string name1 = "Gao" + "Huan" + "Jie"; in fact: String name1 = "Gaohuanjie"; So of course it doesn't take too much time, but what you should be aware of here is that if your string is from another string object, the speed is not that fast, for example:

String firstName = "Gao";
String secondname = "Huan";
String thirdname = "Jie";
String name1 = firstName + Secondname + thirdname;
That's when the JVM does it the way it used to.

StringBuilder: A mutable sequence of characters. This class provides an API that is compatible with StringBuffer, but does not guarantee synchronization (that is, StringBuilder is not thread safe). This class is designed 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 because StringBuilder does not perform synchronization. Each string generator has a certain capacity. You do not need to allocate a new internal buffer as long as the string generator contains a sequence of characters that does not exceed this capacity. This capacity is automatically increased if the internal buffer overflows. Again: It is not safe to use an instance of StringBuilder for multiple threads. If such a synchronization is required, use StringBuffer is recommended.

The difference between StringBuilder and StringBuffer:

StringBuilder is JDK1.5 new class and StringBuffer from JDK1.0; as described above StringBuilder is thread-safe and stringbuffer is threaded, and based on this, it is recommended that you use STRINGBU without considering thread safety Ilder.

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.