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. So to be able to share. Like what:

String str = "abc"; equivalent to char data[] = {' A ', ' B ', ' C '}; string str = new string (data);
StringBuffer: String The string buffer. But cannot be changed. Although it includes a specific sequence of characters at random points in time, some method calls can change the length and content of the sequence.
    appendinsertMethod. These methods can be overloaded to accept arbitrary types of data. Each method effectively converts the given data to a string. The character of the string is then appended or inserted into the string buffer. append insert Method adds a character to the specified point.

    String buffers can be safely used with multiple threads. Be able to synchronize these methods when necessary. As a result, all operations on a 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 aStringBuffercompatible APIs, but does not guarantee synchronization. This class is designed to be used asStringBufferA simple replacement for a string buffer that is used by a single thread (this is a very common situation).

If possible, it is recommended that the class be preferred, since in most implementations it is moreStringBufferbe quick. Span style= "FONT-FAMILY:ARIAL,HELVETICA,SANS-SERIF; Font-weight:normal "> Main operation is Append and insert method. These methods can be overloaded to accept arbitrary types of data. Each method effectively converts the given data to a string. The character of the string is then appended or inserted into the string generator. append method always adds these characters to the end of the generator, while the insert Method adds a character to the specified point.

2. Differences:

Comparison of operating speed: StringBuilder >  stringbuffer.
Suppose 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



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.