What is the difference between StringBuilder and StringBuffer? | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
+ 2

What is the difference between StringBuilder and StringBuffer?

20th Dec 2017, 12:54 PM
Rashmi Adarsh
Rashmi Adarsh - avatar
2 Respostas
+ 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