Full explanation of StringBuilder, StringBuffer, and string relationships

Source: Internet
Author: User

1. String class
The value of string is immutable, which results in a new string object being generated each time a string is manipulated, not only inefficient, but also a significant waste of limited memory space.
String a = "a"; Suppose a points to address 0x0001
A = "B";//re-assigns a point to address 0x0002, but "a" saved in the 0x0001 address still exists, but is no longer the point of a, a has pointed to other addresses.
So the operation of string is to change the assignment address instead of changing the value operation.

2. StringBuffer is a variable class, and thread-safe string manipulation class, and any manipulation of the string it points to does not produce new objects. Each StringBuffer object has a certain buffer capacity, and when the string size does not exceed the capacity, no new capacity is allocated and the capacity is automatically increased when the string size exceeds the capacity.

StringBuffer buf=new StringBuffer (); Assigning a 16-byte long character buffer
StringBuffer buf=new StringBuffer (512); Assigning a 512-byte long character buffer
StringBuffer buf=new StringBuffer ("This is a test")//The string is stored in the buffer, and a 16-byte empty buffer is reserved behind it.

3.StringBuffer
StringBuffer and StringBuilder class functions are basically similar, the main difference is that the StringBuffer class method is multithreaded, secure, and StringBuilder is not thread-safe, in contrast, The StringBuilder class will be slightly faster. The StringBuffer and StringBuilder classes should be used for strings that frequently change values.

4. Thread Safety
StringBuffer Thread Safety
StringBuilder thread is not secure

5. Speed
In general, speed from fast to slow: stringbuilder>stringbuffer>string, this comparison is relative, not absolute.

6. Summary
(1). If you want to manipulate a small amount of data with = String
(2). Operating a large amount of data under a single-threaded operation string buffer = StringBuilder
(3). Multi-threaded operation string buffers operation of large amounts of data = StringBuffer

Full explanation of StringBuilder, StringBuffer, and string relationships

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.