Difference between String, StringBuffer and StringBuilder, stringbuffer

Source: Internet
Author: User

Difference between String, StringBuffer and StringBuilder, stringbuffer
Directory:

1. Immutable String class

2. What should I pay attention to when using String?

3. Differences between String and StringBuilder

4. Differences between StringBuilder and StringBuffer

Immutable String class:

For this non-variability, you need to check the source code. There are several member variables in the String class:

Private final char [] value; // stores the String value

Private final int offset;

Private final int count;

They are all final types. They cannot be changed after you create a String, so this immutable refers to the immutable value of String.

What should I pay attention to when using String?

Because of the immutability of String, you should avoid creating a large number of String objects. for a large number of final String objects, a large amount of memory will be used temporarily, which is very valuable.

Differences between String and StringBuilder

StringBuilder does not create new String objects when performing String connection and other operations. Therefore, we recommend that you convert a String to StringBuilder before performing operations during String operations, which saves memory.

Difference between StringBuilder and StringBuffer

The difference between the two is that StringBuffer is thread-safe, and its methods are locked and mostly used in multithreading. It is recommended to use StringBuilder in a single thread, which will be 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.