String, stringbuilder, stringbuffer

Source: Internet
Author: User

String

The string value is immutable, which leads to a new String object generated every time a string operation is performed, which is not only inefficient, but also a large waste of limited memory space.

String A = "A"; // assume that a points to the address 0x0001 A = "B"; // After the value is re-assigned, a points to the address 0x0002, however, the "A" stored in the address 0x0001 still exists, but it is no longer pointed to by a, and a has pointed to another address.

Therefore, the string operation changes the value assignment address rather than the value change operation.

Therefore, it is best not to use a string that often changes the content, because every time an object is generated, it will affect the system performance, especially when there are more referenced objects in the memory, the GC of JVM will start to work, and the speed will be quite slow.

Stringbuffer

Stringbuffer is a variable class and thread-safe string operation class. Any operation on the string to which it points will not produce new objects. Each stringbuffer object has a certain buffer capacity. When the string size does not exceed the capacity, no new capacity is allocated. When the string size exceeds the capacity, the capacity is automatically increased.

Stringbuffer Buf = new stringbuffer (); // allocate a 16-Byte Character Buffer stringbuffer Buf = new stringbuffer (512 ); // allocate a 512-Byte Character Buffer stringbuffer Buf = new stringbuffer ("this is a test") // stores strings in the buffer, A 16-byte empty buffer is reserved later.

The string buffer can be safely used for multiple threads. These methods can be synchronized as necessary, so all the operations on any specific instance are in serial order, this sequence is consistent with the method call sequence of each involved thread.
The main operations on stringbuffer are:AppendAndInsertMethods to accept any type of data. Each method can effectively convert the given data to a string, and then append or insert the character of the string into the string buffer. The append method always adds these characters to the end of the buffer, while the insert method adds the characters at the specified point.

Stringbuilder

Stringbuilder adds a variable character sequence of 5.0. stringbuffer and stringbuilder have similar functions. The main difference is that the stringbuffer method is multi-threaded and secure, while stringbuilder is NOT thread-safe, in contrast, the stringbuilder class is slightly faster in the case of a single thread. The stringbuffer and stringbuilder classes should be used for strings that frequently change values.

Thread Security

Stringbuffer thread security
Stringbuilder thread is not secure

Speed

In general, the speed is from fast to slow: stringbuilder> stringbuffer> string. This comparison is relative, not absolute.

Summary 

(1). If you want to operate a small amount of data, use = string

(2). A single thread operates on a large amount of data in the string buffer = stringbuilder

(3). multi-threaded operation of a large amount of data under the string buffer = stringbuffer

I am the dividing line of tiantiao

 

 

Reference: http://blog.csdn.net/fmh2011/article/details/18981455

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.