Categories
java

Difference between StringBuffer and StringBuilder in java

Difference between StringBuffer and StringBuilder in java

In this tutorial, we will learn the difference between StringBuffer and StringBuilder in java

StringBuffer and StringBuilder are mostly used for string operations in java. but it  has some difference in nature, let’s see the difference below.

StringBuilder StringBuffer
It is not thread safe,which means It is not synchronizedIt is thread safe, which means it is synchronized
It is faster than StringBufferIt is slower than StringBuilder

StringBuffer Syntax

StringBuffer buffer = new StringBuffer();
//.append is the keyword to concat the strings
buffer.append("");

StringBuilder Syntax

StringBuilder builder = new StringBuilder();
// .append method used to concat string
builder.append("");

Examples

StringBufferhttps://beginnersbug.com/stringbuffer-example-in-java/
StringBuilderhttps://beginnersbug.com/stringbuilder-java/
Reference

https://docs.oracle.com/javase/7/docs/api/java/lang/StringBuffer.html

https://docs.oracle.com/javase/7/docs/api/java/lang/StringBuilder.html