+ 2
What is the difference between StringBuilder and StringBuffer?
2 Answers
+ 5
StringBuilder is faster than StringBufferbecause it's not synchronized.
Here's a simple benchmark test:
public class Main { public static void main(String[] args) { int N = 77777777; long t; { StringBuffer sb = new StringBuffer(); t = System.currentTimeMillis(); for (int i = N; i --> 0 ;) { sb.append(""); } System.out.println(System.currentTimeMillis() - t); } { StringBuilder sb = new StringBuilder(); t = System.currentTimeMillis(); for (int i = N; i --> 0 ;) { sb.append(""); } System.out.println(System.currentTimeMillis() - t); } } }
âââ :)
https://stackoverflow.com/questions/355089/difference-between-stringbuilder-and-stringbuffer
+ 2
stringbuffer is threadsafe, stringbuilder isnt but stringbuilder is faster.