Relationships between StringBuilder, StringBuffer, and string classes in Java

Source: Internet
Author: User

Today, at Csdn's College club, I saw "Java Fundamentals test". Feel like you've learned Java for so long that you want to see what your Java level is. The test results will not say, anyway, is dire can't bear to see.

Look at the results analysis, about StringBuilder, StringBuffer, String class three questions all answer wrong. So I looked up some information and recorded it.

The value of string is immutable, which results in a new string object being generated for each operation on the string. Not only inefficient, but also a lot of waste of limited memory space, StringBuffer is a mutable class. and thread-safe string manipulation classes. No matter what the operation of the string it points to will not produce new objects, the StringBuffer and StringBuilder class functions are basically similar.

1. String class
The value of string is immutable, which results in a new string object being generated for each operation on the string. Not only inefficient, but also a lot of waste of limited memory space.

String a = "a"; If a points to address 0x0001 a = "B";//once again assigns a point to address 0x0002. However, the "a" saved in the 0x0001 address still exists. But it is no longer a point, 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

StringBuffer is a variable class, and thread-safe string manipulation class. No matter what the manipulation of the string it points to, the new object is not produced. Each StringBuffer object has a certain buffer capacity when the string size does not exceed the capacity. No new capacity is allocated, and when the string size exceeds capacity, the capacity is added on its own initiative.

StringBuffer buf=new StringBuffer (); Allocates a 16-byte long character buffer StringBuffer buf=new StringBuffer (512); Allocates a character buffer of 512 bytes long 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.StringBuilder
StringBuffer and StringBuilder functions are basically similar. The main difference is that the method of StringBuffer class is multithreaded and secure. While StringBuilder is not thread-safe, the StringBuilder class is a little faster than it is. The StringBuffer and StringBuilder classes should be used for strings that often change values.

4. Thread Safety

StringBuffer Thread Safety
StringBuilder thread is not secure

5. Speed
Under normal circumstances, speed from fast to slow: stringbuilder>stringbuffer>string, such a comparison is relative, not absolute.



6. Summary
(1). Suppose 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


Relationships between StringBuilder, StringBuffer, and string classes 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: 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.