Differences between string, stringbuffer, and stringbuilder in Java

Source: Internet
Author: User

I think there are three differences between string and stringbuffer:

(1) What does string mean when we create a String object str1 = "ABC"? In this case, a space is opened to store ABC and str1 points to it, now I "re-assign" str1 = "def" to str1. In this case, a new space is created to store def, and str1 points to the new space. In this case, stringbuffer directly modifies the value in the starting space. Therefore, string is the changed address, while stringbuffer is the modified value.

(2) During the determination, the string object overwrites the equals () method of the object, compares the value rather than the address, while the stringbuffer does not override this method, and identifies the address.

(3) When connecting strings, stringbuffer is usually used, because it is more efficient, such as string a = new string ("hello"); A + = "world "; in fact, during the connection, A is converted into a stringbuffer object, and then connected to the world using the append method of stringbuffer, and finally converted to a string object.

 

The difference between stringbuffer and stringbuilder is that stringbuffer is thread-safe, while stringbuilder is insecure.

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.