String, StringBuffer and StringBuilder differences and performance analysis

Source: Internet
Author: User

1. Performance comparison:StringBuilder > StringBuffer > String

2. String < (stringbuffer,stringbuilder) the cause

String: Strings constant

StringBuffer: String variable

StringBuilder: String variable

3. Source Code Analysis

String: Look at the source, the declaration of the string class is: Public final, so it is clear that the final value is not changed, so if we use string to manipulate strings, once the value of the string is changed, Creates a new space in memory to hold the new string, so it is inefficient. So, generally involved in string manipulation, we generally use StringBuffer or StringBuilder.

StringBuffer and StringBuilder both integrate Abstractstringbuilder, and StringBuffer most methods are synchronized, which is thread-safe, And StringBuilder is not, so, we look at the API can know, StringBuilder can operate stringbuffer, but StringBuffer can not operate StringBuilder, which is the cause of the thread. Therefore, it is conceivable that the efficiency of stringbuffer is certainly not StringBuilder, because stringbuffer to maintain a synchronous lock, which is sure to consume some resources.

String, StringBuffer and StringBuilder differences and performance analysis

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.