String, StringBuilder, StringBuffer differences

Source: Internet
Author: User

In Java, String, StringBuffer, and StringBuilder are frequently used strings in programming, and the differences between them are frequently asked questions in the interview. Now summarize and see how they differ from the same.
1. Variable and non-changeable
The string class uses a character array to hold strings, as follows, because there is a "final" modifier, so you know that the string object is immutable.
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.
Char[] value;
2. Whether multithreaded security
The objects in string are immutable and can be understood as constants, apparently thread-safe.
Abstractstringbuilder is the common parent of StringBuilder and StringBuffer, and defines some basic operations for strings, such as expandcapacity, append, insert, indexof, and other public methods.
StringBuffer is thread-safe because it adds a synchronous lock to a method or a synchronous lock on a method that is called. See the following source code:

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.
3.StringBuilder and StringBuffer in common
StringBuilder and StringBuffer have public parent class Abstractstringbuilder (abstract Class).
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.
Finally, if the program is not multithreaded, then using StringBuilder is more efficient than stringbuffer.



String, StringBuffer, StringBuilder differences

StringBuffer, StringBuilder, and string are also used to represent strings. The string class is an immutable class, and any change to string will cause a new string object to be generated; StringBuffer is a mutable class, and any change to the string it refers to will not produce new objects. Since the variable and immutable are there, why is there a StringBuilder? Believe that the beginning of you, in the Append, the general will choose StringBuffer Bar!

First of all, the collection of the story, Hashtable is thread-safe, many methods are synchronized methods, and HashMap is not thread-safe, but its performance in single-threaded programs is higher than Hashtable. The difference between the StringBuffer and StringBuilder classes is the same, their principle and operation is basically the same, the difference is that Stringbufferd supports concurrent operations, linear security, suitable for multi-threaded use. StringBuilder does not support concurrent operations, which are linearly unsafe and not suitable for use in multi-threading. The newly introduced StringBuilder class is not thread-safe, but its performance in a single thread is higher than stringbuffer.

String, StringBuilder, StringBuffer differences

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.