The difference between String,stringbuffer and StringBuilder

Source: Internet
Author: User

1. Definition:

String: String class represents strings. All string literals (such as) in a Java program "abc" are implemented as instances of this class. Strings are constants, and their values cannot be changed after they are created. A string buffer supports variable strings. Because the String object is immutable, it can be shared. For example:

String str = "abc"; equivalent to char data[] = {' A ', ' B ', ' C '}; string str = new string (data);
StringBuffer: String The string buffer, but cannot be modified. Although it contains a specific sequence of characters at any point in time, some method calls can change the length and content of the sequence.
    appendinsertmethod, which overloads these methods to accept arbitrary types of data. Each method effectively converts the given data into a string, and then appends or inserts the character of the string into the string buffer. append insert Method adds a character at the specified point.
    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.
stringbuilder: A variable sequence of characters. This class provides an API that is compatible with  stringbuffer   stringbuffer   stringbuffer   fast.  /span> main operation is  append   and  insert   method, you can overload these methods to accept any type of data. Each method effectively converts the given data into a string, and then appends or inserts the character of the string into the string builder.  append   method always adds these characters to the end of the generator, while the  insert   Method adds a character at the specified point. 2. Difference:
Comparison of execution speed: StringBuilder >  stringbuffer;
If you want to manipulate a small amount of data with = String
 single-threaded operation string buffer Large amount of data under operation = StringBuilder   
Multi-threaded operation string buffers operation of large amounts of data = StringBuffer



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

The difference between String,stringbuffer and StringBuilder

Related Article

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.