The difference between string, StringBuffer, StringBuilder in Java

Source: Internet
Author: User
Tags stringbuffer

Differences in string, StringBuffer, StringBuilder in Java (RPM)

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:

1   Public synchronizedStringBuffer Reverse () {2      Super. reverse ();3      return  This;4  }5  6   Public intindexOf (String str) {7      returnIndexOf (str, 0);//There is a public synchronized int indexOf (String str, int fromIndex) method8}

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.

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