Home > Java > javaTutorial > body text

The difference between stringbuffer and stringbuilder

(*-*)浩
Release: 2019-06-04 13:54:07
Original
3532 people have browsed it

Whether you are working on Java or Android, you cannot avoid encountering this problem. In fact, you usually don’t struggle with it during the development process. This question is a must-have classic question in interviews. If you have time today, I will summarize it.

The difference between stringbuffer and stringbuilder

The difference between StringBuffer and StringBuilder

The methods and functions in StringBuffer and StringBuilder are completely equivalent, except for the methods in StringBuffer Most of them are modified with the synchronized keyword, so they are thread-safe, support concurrent operations, and are suitable for use in multi-threads. StringBuilder does not support concurrent operations, is linearly unsafe, and is not suitable for use in multi-threads. The newly introduced StringBuilder class is not thread-safe, but its performance in a single thread is higher than StringBuffer. (Recommended learning: JAVA video tutorial)

Next, I directly paste the code of the test process and results, which is clear at a glance:

public class StringTest {
 
	public static String BASEINFO = "Mr.Y";
	public static final int COUNT = 2000000;
 
	/**
	* 执行一项String赋值测试
	*/
	public static void doStringTest() {
 
		String str = new String(BASEINFO);
		long starttime = System.currentTimeMillis();
		for (int i = 0; i < COUNT / 100; i++) {
			str = str + "miss";
		}
		long endtime = System.currentTimeMillis();
		System.out.println((endtime - starttime)
				+ " millis has costed when used String.");
	}
 
	/**
	* 执行一项StringBuffer赋值测试
	*/
	public static void doStringBufferTest() {
 
		StringBuffer sb = new StringBuffer(BASEINFO);
		long starttime = System.currentTimeMillis();
		for (int i = 0; i < COUNT; i++) {
			sb = sb.append("miss");
		}
		long endtime = System.currentTimeMillis();
		System.out.println((endtime - starttime)
				+ " millis has costed when used StringBuffer.");
	}
 
	/**
	* 执行一项StringBuilder赋值测试
	*/
	public static void doStringBuilderTest() {
 
		StringBuilder sb = new StringBuilder(BASEINFO);
		long starttime = System.currentTimeMillis();
		for (int i = 0; i < COUNT; i++) {
			sb = sb.append("miss");
		}
		long endtime = System.currentTimeMillis();
		System.out.println((endtime - starttime)
				+ " millis has costed when used StringBuilder.");
	}
 
	/**
	* 测试StringBuffer遍历赋值结果
	* 
	* @param mlist
	*/
	public static void doStringBufferListTest(List<String> mlist) {
		StringBuffer sb = new StringBuffer();
		long starttime = System.currentTimeMillis();
		for (String string : mlist) {
			sb.append(string);
		}
		long endtime = System.currentTimeMillis();
		System.out.println(sb.toString() + "buffer cost:"
				+ (endtime - starttime) + " millis");
	}
 
	/**
	* 测试StringBuilder迭代赋值结果
	* 
	* @param mlist
	*/
	public static void doStringBuilderListTest(List<String> mlist) {
		StringBuilder sb = new StringBuilder();
		long starttime = System.currentTimeMillis();
		for (Iterator<String> iterator = mlist.iterator(); iterator.hasNext();) {
			sb.append(iterator.next());
		}
 
		long endtime = System.currentTimeMillis();
		System.out.println(sb.toString() + "builder cost:"
				+ (endtime - starttime) + " millis");
	}
 
	public static void main(String[] args) {
		doStringTest();
		doStringBufferTest();
		doStringBuilderTest();
 
		List<String> list = new ArrayList<String>();
		list.add(" I ");
		list.add(" like ");
		list.add(" BeiJing ");
		list.add(" tian ");
		list.add(" an ");
		list.add(" men ");
		list.add(" . ");
 
		doStringBufferListTest(list);
		doStringBuilderListTest(list);
	}
 
}
Copy after login

Look at the execution results:

2711 millis has costed when used String.
211 millis has costed when used StringBuffer.
141 millis has costed when used StringBuilder.
 I  like  BeiJing  tian  an  men  . buffer cost:1 millis
 I  like  BeiJing  tian  an  men  . builder cost:0 millis
Copy after login

As can be seen from the above results, regardless of multi-threading, when using String objects (I put Count/100), the execution time is longer than the other two. High, and the difference between using StringBuffer objects and using StringBuilder objects is also obvious. It can be seen that if our program is running in a single thread, or there is no need to consider thread synchronization issues, we should give priority to using the StringBuilder class; if we want to ensure thread safety, it is naturally StringBuffer.

It can be seen from the test results of the following List that except for the different support for multi-threading, there is almost no difference in the usage and results of these two classes.

For more JAVA related technical articles, please visit the JAVA Development Tutorial column to learn!

The above is the detailed content of The difference between stringbuffer and stringbuilder. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template