String, StringBuffer, StringBuilder differences in Java

Source: Internet
Author: User

1, String

The value of the string variable cannot be changed, and if the value of the string variable is to be changed, the virtual machine first traverses the string constant in the method area, and if there is a required value, the virtual machine assigns the address of the constant value directly to the string variable, if no such value exists. The virtual machine will additionally divide a block of memory space in the method area to store string constants, and then assign the address to the string variable.

Therefore, the contents of the string class can not be changed after the declaration, the change is only the point of its memory address.

2, StringBuffer

StringBuffer is used to store string objects in a buffer, and the contents of an object can be changed. and is thread-safe.

StringBuffer is not instantiated like a string by direct assignment, he must be instantiated by constructing the method.

3, StringBuilder

StringBuilder is the same as StringBuffer, the contents of the string object can be changed, but the StringBuilder thread is unsafe.

StringBuilder is not instantiated like a string by direct assignment, he must be instantiated by constructing the method.

4. Assign Value

String takes a + connection string or character; StringBuilder or StringBuffer connects characters or strings through the Append method.

5, StringBuffer or StringBuilder common methods

Append, insert, delete, replace, substring, indexOf (find), reverse

String, StringBuffer, StringBuilder differences in Java

Related Article

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.