Small knowledge, big challenge! This paper is participating in theEssentials for programmers”Creative activities

This paper has participated inProject DigginTo win the creative gift package and challenge the creative incentive money.

📖 preface

Good attitude, not so tired. In a good mood, all you see is beautiful scenery.Copy the code

** ‘” for a while can not solve the problem, then take advantage of this opportunity to see their limitations, to their own a right.” As the old saying goes, it’s easy to let go. If you are distracted by something, learn to disconnect. Cut out the paranoia, the trash in the community, and get rid of the negative energy. Good attitude, not so tired. In a good mood, all you see is beautiful scenery.


The paper

First, the speed, or execution speed, in this case: StringBuilder > StringBuffer > String

  1. String is the slowest.

String is a String constant, while StringBuilder and StringBuffer are String variables, meaning that a String object cannot be changed once created, whereas the latter two objects are variables and can be changed. Take the following code for example:

  1. StringBuilder and StringBuffer objects are variables, and manipulating variables directly changes the object without creating or recycling it, so it’s much faster than String
  1. StringBuilder is thread-safe, while StringBuffer is thread-safe. If a StringBuffer object is used by multiple threads in the StringBuffer, many methods in StringBuffer can have the synchronized keyword, The StringBuilder method doesn’t have this keyword, so it’s not thread-safe, and it’s possible to do something wrong. So if the operation is multithreaded, use StringBuffer, but in single-threaded cases, the faster StringBuilder is recommended.

Go straight to code

package com.test;

/ * * * *@Description: Why stringBuffer or StringBuilder should be used to concatenate strings -- not strings -- and the basics of StringBuffer A StringBuffer is thread-safe * if a StringBuffer object is used by multiple threads in the StringBuffer, many of the methods in the StringBuffer can carry the synchronized keyword * so that it is thread-safe. However, StringBuilder methods do not have this keyword, so there is no guarantee of thread-safety and the possibility of error operations. * So if the operation is multithreaded, use StringBuffer, but in single-threaded cases the faster StringBuilder is recommended. * * The stringBuilder append method is return this, while the String append method is return new String * * Because string uses the final char[] value array to store the contents of the string, The stringBuilder class value array is not final, it is mutable, so there is no need to regenerate a new object. But array enlargement is simply replacing a larger array@ClassName: Test.java
 * @author ChenYongJia
 * @DateJune 26, 2019 20:23 PM *@Email [email protected]
 */
public class Test {

	public static void main(String[] args) {
		String str = "";
		StringBuffer sb = new StringBuffer();// Thread safety
		StringBuilder sbBuilder = new StringBuilder();// The thread is not safe
		long start = 0L;
		long end = 0L;
		start = System.currentTimeMillis();
		for (int i = 0; i < 99999; i++) {// loop 100,000 times
			str = str + "Let's test this out and see what the difference is!";
		}
		end = System.currentTimeMillis();
		System.out.println("The time to use string is :" + (end - start) + "毫秒!");

		start = System.currentTimeMillis();
		for (int i = 0; i< 99999; i++) {// loop 100,000 times
			sb.append("Let's test this out and see what the difference is!");
		}
		end = System.currentTimeMillis();
		System.out.println("StringBuffer is used for :" + (end - start) + "毫秒!");

		start = System.currentTimeMillis();
		for (int i = 0; i < 99999; i++) {// loop 100,000 times
			sbBuilder.append("Let's test this out and see what the difference is!");
		}
		end = System.currentTimeMillis();
		System.out.println("Time to use StringBuilder is :" + (end - start) + "毫秒!"); }}Copy the code

Let’s look at the execution result

The String is concatenated in 223 seconds. The other two are milliseconds. I don’t need to say more


conclusion

  • String: Applies to a small number of String operations

  • StringBuilder: This is suitable for large operations in the character buffer under a single thread

  • StringBuffer: Applies to multiple threads where a large number of operations are performed in the character buffer

  • The StringBuilder class append method is return this, and the String class append method is return New String

  • Because string uses the final char[] value array to store the contents of the string, each change is a return new String that returns a new string. It takes time to generate a new string object and allocate memory. The Value array of the StringBuilder class is not final, it is mutable and does not need to regenerate new objects, but array expansion is simply replacing a larger array

Finally, thank you for your patience to watch the end, the original is not easy, leave a point like collection is your biggest encouragement to me!


🎉 summary:

  • For more references, see here:The Blog of Chan Wing Kai

  • Like the small partner of the blogger can add a concern, a thumbs-up oh, continue to update hey hey!