In Java, the difference between string,stringbuffer,stringbuilder

Source: Internet
Author: User

First of all, the difference between string,stringbuffer

There are two main aspects, the first is the thread safety aspect, the second is the efficiency aspect

Thread Safety aspects:

string is not thread-safe, which means that when a string is shared by different threads, it needs to be synchronized, otherwise there will be different values for different threads, that is, the thread is unsafe

StringBuffer are thread-safe and are automatically synchronized when shared among different threads, and do not require manual synchronization.

Efficiency aspects:

Each time a string is changed, a variable is re-established, the changed value is given, and the JVM's GC mechanism is triggered after multiple changes, occupying resources

StringBuffer changes the original variable every time it changes, so it's not more efficient when you change it multiple times.

Second, the difference between StringBuffer and StringBuilder

StringBuilder is thread insecure, but is more efficient than stringbuffer when used in a single-threaded environment

Comprehensive:

The order of precedence for use should be, StringBuilder >stringbuffer>string

In Java, the difference between string,stringbuffer,stringbuilder

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.