The difference between string, StringBuilder, and StringBuffer in Java

Source: Internet
Author: User
Tags stringbuffer

---restore content starts---

  • string literal constant
  • StringBuffer string variable (thread safe)
  • StringBuilder string variable (non-thread safe)

  String is a constant, and the value of the object has been assigned and cannot be changed. If you do not want to change, you can re-create another object, and then assign the value to the newly created object.

  StringBuffer is a variable and is a quantity that can change at any time. If the StringBuffer object is changed, each result will be manipulated against the StringBuffer object instead of generating a new object. Therefore, it is recommended to use StringBuffer if the general string changes frequently.

StringBuilder and StringBuffer class functions are basically similar, the method is the same, the main difference is that StringBuffer class method is multithreaded security, and StringBuilder is not thread-safe, The StringBuilder class would be slightly faster than that.

---restore content ends---

The difference between string, StringBuilder, and StringBuffer in Java

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: 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.