+ 2

What is the difference between StringBuilder and StringBuffer?

20th Dec 2017, 12:54 PM
Rashmi
Rashmi - avatar
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
20th Dec 2017, 1:00 PM
James16
James16 - avatar
+ 2
stringbuffer is threadsafe, stringbuilder isnt but stringbuilder is faster.
20th Dec 2017, 12:57 PM
Jeremy
Jeremy - avatar