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 synchronized | It is thread safe, which means it is synchronized |
It is faster than StringBuffer | It 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
StringBuffer : https://beginnersbug.com/stringbuffer-example-in-java/
StringBuilder : https://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