The difference between Java basic knowledge 2--string,stringbufffer,stringbuilder

Source: Internet
Author: User

The difference between String,stringbufffer,stringbuilder

1. Variable Immutable aspects

The string class uses a character array to hold strings, and the final decoration is certainly immutable, and when strings are manipulated, the value of our string is changed to create more space in memory to hold the new string, that is, when the string is manipulated, In fact, the creation of new objects, and the original object will be garbage collected by GC, once encountered complex operation, string very inefficient

Private Final Char value[];

Both StringBuilder and StringBuffer inherit from the Abstractstringbuilder class, and in the Abstractstringbuilder, they are also used to hold strings in a character array, as follows, that both of these objects are mutable. Whenever we use them to manipulate strings, we actually operate on an object. So, generally involved in string manipulation, we generally use StringBuffer or StringBuilder

Char [] value;

2. Thread Safety aspects:

String is immutable, of course it's safe.

Abstractstringbuilder (abstract Class) is the common parent class of StringBuilder and StringBuffer, which defines some basic operations of strings, such as expandcapacity, append, insert, IndexOf and other public methods. One of the differences between an abstract class and an interface is that the public methods of some subclasses can be defined in an abstract class, and subclasses only need to add new functionality, and do not need to repeat the existing methods, whereas interfaces are simply definitions of the declarations and constants of the methods.

StringBuilder, StringBuffer methods will call public methods in Abstractstringbuilder, such as Super.append (...). Just stringbuffer will add synchronized keywords to the method and synchronize.

StringBuffer has a synchronous lock on the method or a synchronous lock on the calling method, so it is thread-safe and often used for multithreading security. Because the StringBuffer to maintain the sync lock, this must consume some resources, performing slower than StringBuilder.

 Public synchronized StringBuffer Reverse () {    Super. Reverse ();     return  This ;}  Public int indexOf (String str) {    return indexOf (str, 0);        // There is a public synchronized int indexOf (String str, int fromIndex) method }

StringBuilder does not have a synchronous lock on the method, so it is non-thread safe. But if it is a single thread, the old can be used directly with StringBuilder.

3. Usage scenario (efficiency: stringbuilder>stringbuffer>string)

    • If you want to manipulate a small amount of data with = String
    • Manipulating large amounts of data under a single-threaded operation string buffer = StringBuilder
    • Multi-threaded operation string buffers operation of large amounts of data = StringBuffer

4. The following methods are commonly used by StringBuilder

StringBuilder Append (parameter)-appends the content to the end of the current StringBuilder object;

StringBuilder Insert (position, parameter)-inserts the content into the specified position of the StringBuilder object; Str.insert (4, "!"); Insert in 5th Position!

StringBuilder Delete (initial position, end position)--delete the offset string

String toString ()-Converts the Sringbuilder object to a String object;

int length ()--Gets the length of the string;

Just Jiangzi.

The difference between Java basic knowledge 2--string,stringbufffer,stringbuilder

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.