Fully explain the relationships between StringBuilder, StringBuffer, and String classes in java, bufferbuilder

Source: Internet
Author: User

Fully explain the relationships between StringBuilder, StringBuffer, and String classes in java, bufferbuilder
Relationship between StringBuilder, StringBuffer, and String classes

In java, String, StringBuffer, and StringBuilder are frequently used String classes in programming. In the previous blog, we were familiar with the features and usage of String strings, what kind of string classes are StringBuffer and StringBuilder ?? What are the differences and relationships between them ?? This question is often asked during the interview. Let's summarize it and see their differences and similarities.

1. Variable and immutable

1) Use a character array in the String class to save the string. The following figure shows that the String object isImmutable.

    Private final char value [];

The String value isImmutableAs a result, each String operation will generate a new String object, which is not only inefficient, but also wastes a lot of limited memory space.

1 String a = "a"; // assume that a points to the address 0x00012 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.

2) Both StringBuilder and StringBuffer inherit from the AbstractStringBuilder class. In AbstractStringBuilder, character arrays are also used to save strings. The following is the result. We can see that both objects are variable.

Char [] value;

StringBuffer is a variable class and thread-safe string operation class. Any operation on the string it pointsNo new object will be generated. Each StringBuffer object has a certain numberBuffer CapacityWhen 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.

1 StringBuffer buf = new StringBuffer (); // allocate a 16-Byte Character Buffer 2 StringBuffer buf = new StringBuffer (512 ); // allocate a 512-Byte Character Buffer 3 StringBuffer buf = new StringBuffer ("this is a test") // stores strings in the buffer, A 16-byte empty buffer is reserved later.

The StringBuffer and StringBuilder classes have similar functions. The main difference is that the StringBuffer class methods are multithreading and secure, while StringBuilder is NOT thread-safe. In contrast, the StringBuilder class is slightly faster.The StringBuffer and StringBuilder classes should be used for strings that frequently change values.

2. multi-thread security?

The objects in the String are unchangeable and can be understood as constants,Apparently thread security.

AbstractStringBuilder is a common parent class of StringBuilder and StringBuffer. It defines some basic string operations, such as expandCapacity, append, insert, indexOf and other public methods.

StringBuffer adds a Synchronous lock to the method or a Synchronous lock to the called method.Thread-safe. See the following source code:

 

1 public synchronized StringBuffer reverse () {2 super. reverse (); 3 return this; 4} 5 6 public int indexOf (String str) {7 return indexOf (str, 0); // public synchronized int indexOf (String str, int fromIndex) Method 8}

 

StringBuilder does not apply a synchronization lock to the method, so yesNon-thread-safe.

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>

StringBuffer is thread-safe, which means they haveSynchronization MethodTo control access, so that only one thread can access a StringBuffer Object synchronization code at the same time. Therefore, StringBuffer objects are usually safe in multi-threaded environments. Multiple Threads can be used to access the same StringBuffer objects at the same time.

The StringBuilder class is very similar to the StringBuffer, but the difference lies in its accessNot synchronizedTherefore, it is not thread-safe. Due to non-synchronization, StringBuilder has better performance than StringBuffer. Therefore, if you work in a single-threaded environment, using StringBuilder instead of StringBuffer may have higher performance. This is similar to other cases, such as the local variable of StringBuilder (a variable in a method). Only one thread accesses a StringBuilder object.

 

3. Speed comparison between StringBuffer and StringBuilder

In general, the speed is from fast to slow: StringBuilder> StringBuffer> String. This comparison is relative, not absolute. (Whether the program is single-threaded or multi-threaded)

Next, I directly paste the code for the test process and result to see at a glance:

1 package com. hysum. test; 2 3 public class StringTest {4 final static int time = 50000; // number of cycles 5/* 6 * String test method 7 */8 public void test (String s) {9 long begin = System. currentTimeMillis (); // get the current system time (in milliseconds), start 10 for (int I = 0; I <time; I ++) {11 s + = "add"; 12} 13 long over = System. currentTimeMillis (); // gets the current System time (in milliseconds) and ends 14 systems. out. println ("operation" + s. getClass (). the getName () + "type is used for:" + (over-begin) + "millisecond "); 15} 16/* 17 * StringBuffer test Method 18 */19 public void test (StringBuffer s) {20 long begin = System. currentTimeMillis (); 21 for (int I = 0; I <time; I ++) {22 s. append ("add"); 23} 24 long over = System. currentTimeMillis (); 25 System. out. println ("operation" + s. getClass (). getCanonicalName () + "type used for:" + (over-begin) + "millisecond "); 26} 27/* 28 * StringBuilder class test method 29 */30 public void test (StringBuilder s) {31 long begin = System. currentTimeMillis (); 32 for (int I = 0; I <time; I ++) {33 s. append ("add"); 34} 35 long over = System. currentTimeMillis (); 36 System. out. println ("operation" + s. getClass (). the getName () + "type is used for:" + (over-begin) + "millisecond "); 37} 38 39/* String concatenation test */40 public void test2 () {// operation String object reference addition type time 41 String s2 = "abcd"; 42 long begin = System. currentTimeMillis (); 43 for (int I = 0; I <time; I ++) {44 String s = s2 + s2 + s2; 45} 46 long over = System. currentTimeMillis (); 47 System. out. println ("Operation String object reference addition type used for:" + (over-begin) + "millisecond"); 48} 49 public void test3 () {// operation string addition time 50 long begin = System. currentTimeMillis (); 51 for (int I = 0; I <time; I ++) {52 String s = "abcd" + "abcd" + "abcd "; 53} 54 long over = System. currentTimeMillis (); 55 System. out. println ("Operation String addition time:" + (over-begin) + "millisecond"); 56} 57 public static void main (String [] args) {58 // TODO Auto-generated method stub59 String s1 = "abcd"; 60 StringBuffer st1 = new StringBuffer ("abcd "); 61 StringBuilder st2 = new StringBuilder ("abcd"); 62 StringTest tc = new StringTest (); 63 tc. test (s1); 64 tc. test (st1); 65 tc. test (st2); 66 tc. test2 (); 67 tc. test3 (); 68} 69 70}

Running result:

Result Analysis:

From the above results, we can see that the execution time is much higher than the other two when using the String object without considering multithreading. The difference between using the StringBuffer object and using the StringBuilder object is also obvious; taking the String type as an example, the Operation String object reference addition type takes much longer than the direct/Operation String addition. It can be seen that if our program runs in a single thread, or we do not need to consider the thread synchronization problem, we should first use the StringBuilder class; if we want to ensure thread security, it is naturally StringBuffer; you can directly operate strings without string reference.

4. Commonalities Between StringBuilder and StringBuffer

StringBuilder and StringBuffer have common parent classes AbstractStringBuilder (Abstract class).

The StringBuilder and StringBuffer Methods call the public methods in AbstractStringBuilder, such as super. append (...).Only StringBuffer will add the synchronized keyword to the method for synchronization.

Let's take a look at their main methods ~

Method Description
StringBuffer append (parameter) Append content to the end of the current StringBuffer object, similar to the string connection
StringBuffer deleteCharAt (int index) Delete the characters at the specified position, and then create a new string for the remaining content.
StringBuffer insert (location, parameter) Insert content into the StringBuffer object and form a new string.
StringBuffer reverse () Reverse the content in the StringBuffer object and form a new string.
Void setCharAt (int index, char ch) The new ch character is used to change the index value to the index position in the object.
Void trimToSize () The storage space in the StringBuffer object is reduced to the same length as the String length to reduce space waste, which is the same as the String trim ().
StringBuffer delete (int start, int end) Deletes a string from a specified region.
StringBuffer replace (int start, int end, String s) Replaces the string of the specified region with a new string.
Void setlength (int n) Set the string buffer size
Int capacity () Obtains the size of a string.
Void ensureCapacity (int n) Make sure that the capacity is at least equal to the specified minimum value. If the current capacity is smaller than this parameter, allocate a new internal array with a larger capacity. The new capacity is large.
GetChars (int start, int end, char chars [], int charStart ); Copy the substring of a string to an array.

The following is a sample code for each method:

1 public static void main (String [] args) {2 // TODO Auto-generated method stub 3 StringBuilder str = new StringBuilder ("Learning java programming "); 4 5 // Method 6 for adding the string content // append (parameter), append the content to the end of the current object 7 str. append ("learning makes me happy"); 8 System. out. println ("Append content to the end of the current object:" + str); 9 // insert (location, parameter), insert content in the object 10 str. insert (10, ','); 11 System. out. println ("insert content in the object:" + str); 12 13 // Method for operating the string content 14 // delete (int start, int end ), delete the string 15 str in the specified region. Delete (11, 17); 16 System. out. println ("Delete the string of the specified region:" + str); 17 // deleteCharAt (int index), delete the character of the specified position 18 str. deleteCharAt (10); 19 System. out. println ("Delete the character at the specified position:" + str); 20 // setCharAt (int index, char newChar ), the character whose index value is changed to the index position in the object is the new character ch21 str. setCharAt (3, 'J'); 22 System. out. println ("the new character ch:" + str); 23 // replace (int start, int end, String s ), replace string 24 str in the specified region with a new string. replace (4, 7, "AVA"); 25 Sys Tem. out. println ("Replace the string of the specified region with the new string:" + str); 26 // reverse the reverse () content 27 str. reverse (); 28 System. out. println ("content inversion:" + str); 29 // copy the substring of the string to the array. 30 char [] ch = new char [5]; 31 str. getChars (0, 4, ch, 0); 32 System. out. println ("copy the substring of the string to the array:" + Arrays. toString (ch); 33 34 35 36 37 StringBuilder str2 = new StringBuilder (30); // create a string with a length of 30 38 str2.append ("JAVA"); 39 System. out. println ("String length:" + str2.length (); // length (), get String length 40 System. out. println ("string capacity:" + str2.capacity (); // capacity (), obtain the string capacity 41 // Method 42 for string space // setLength (int newSize), set the string buffer size 43 str2.setLength (20); 44 System. out. println ("String Length:" + str2.length (); 45 System. out. println ("string capacity:" + str2.capacity (); 46 // ensureCapacity (int n), reset the size of string capacity 47 str2.ensureCapacity (20); 48 System. out. println ("String Length:" + str2.length (); 49 System. out. println ("string capacity:" + str2.capacity (); 50 str2.ensureCapacity (35); 51 System. out. println ("String Length:" + str2.length (); 52 System. out. println ("string capacity:" + str2.capacity (); 53 // trimToSize (), the storage space is reduced to the same length as the string 54 str2.trimToSize (); 55 System. out. println ("String Length:" + str2.length (); 56 System. out. println ("string capacity:" + str2.capacity (); 57 58 59} 60 61}

Running result:

Result Analysis:

1. When using a range of parameter methods,Note that the range includes the beginning, not the end!

2. The position of the insert method is the one you want to insert, not the previous one!

3. In the getChars method, note that the length of the character array must be greater than or equal to the length of the Character Between In in and end!

4. length is the length of the string content, while capacity is the length of the string capacity (including the cache area!

5. The ensureCapacity method ensures that the capacity is at least equal to the specified minimum value. If the current capacity is smaller than this parameter, allocate a new internal array with a larger capacity (not the value you specified, the system automatically allocates a space ). If the current capacity is not smaller than this parameter, the capacity remains unchanged.

6. trimToSize (): the storage space is reduced to the same length as the string length.Avoid space waste!

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

 

References:

Http://www.jb51.net/article/33398.htm

Http://blog.csdn.net/mad1989/article/details/26389541

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.