Difference between String, StringBuffer and StringBuilder, stringbuffer

Source: Internet
Author: User

Difference between String, StringBuffer and StringBuilder, stringbuffer

Differences between String, StringBuffer, and StringBuilder


1. The String class is an unchangeable class, that is, once a String object is created, the Character Sequence containing this object cannot be changed.


2. In the process of String concatenation, the String efficiency is lower than that of StringBuffer and StringBuilder.


3. StringBuilder has better performance than StringBuffer


4. StringBuffer is thread-safe and StringBuilder is thread-unsafe.


What is the difference between stringbuffer and stringbuilder?

1. Comparison of execution speed: StringBuilder> StringBuffer
2. stringBuffer and StringBuilder are string variables and changeable objects. Each time we use them to operate on strings, they are actually operated on an object, unlike creating some objects like strings for operations, the speed is faster.
3. StringBuilder: thread-safe
StringBuffer: thread-safe
When we use it in string buffering by multiple threads, the JVM cannot ensure that the StringBuilder operation is safe. Although its speed is the fastest, it can ensure that StringBuffer can be operated correctly. Of course, in most cases, we perform operations in a single thread. In most cases, we recommend using StringBuilder instead of StringBuffer, which is the reason for speed.

Summary: 1. If you want to operate a small amount of data, use = String
2. A single thread operates on a large amount of data in the string buffer = StringBuilder
3. multi-threaded operations on a large amount of data in the string buffer = StringBuffer

C # What is the difference between String and StringBuffer? What is the difference between StringBuffer and StringBuilder?

String is fixed, which means that it consumes resources to connect strings in the String.

StringBuffer can be used as a buffer, so it is generally used as a string buffer.

The only difference between StringBuilder and StringBuffer is that the Builder thread is not secure and thus faster.

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.