<div dir="ltr" style="text-align: justify;">
<div class="separator" style="clear: right; float: right; margin-bottom: 1em; margin-left: 1em; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2017/02/stringbuffer-vs-stringbuilder.jpg" width="320" height="180" border="0" /></div>
<p>In this article we are going to discuss difference between StringBuilder and StringBuffer class in java. This is one of frequently asked question in <a href="https://dineshonjava.com/core-java-interview-questions/">core java interview question</a>. In very short StringBuffer is thread-safe means all methods in StringBuffer class are synchronized but StringBuilder is not thread-safe and both classes are mutable unlike String. StringBuffer is available from JDK 1.0 but StringBuilder is available from JDK 1.5. Actually StringBuilder is the copy of StringBuffer without thread safety. In java there are three classes related to string manipulation <a href="https://dineshonjava.com/differences-between-string-stringbuffer-and-stringbuilder-in-java/">String, StringBuffer and StringBuilder</a> in the java.lang package.</p>
<p>Out of these three classes <a href="https://dineshonjava.com/why-string-is-immutable-and-final-in-java/">String is immutable by nature</a>. while StringBuilder and StringBuffer are mutable version of String. String immutability nature because of many reasons related like Security, String Pool, and Performance.</p>
<div style="background-color: #f2f9fc; border: 1px solid #c9e6f2; border-radius: 3px; padding: 16px; line-height: 1.45;">
<p><span style="color: red; font-size: x-large; text-align: center;"><b>Popular Tutorials</b></span></p>
<ul style="text-align: left;">
<li><b><a href="https://dineshonjava.com/spring-tutorial/"><em><strong>Spring Tutorial</strong> </em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-web-mvc-framework-chapter-38/"><strong><em>Spring MVC Web Tutorial </em></strong></a></b></li>
<li><b><a href="https://dineshonjava.com/introduction-to-spring-boot-a-spring-boot-complete-guide/"><strong>Spring Boot Tutorial</strong> </a></b></li>
<li><b><a href="https://dineshonjava.com/spring-security-take-baby-step-to-secure/"><em>Spring Security Tutorial</em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-tutorial-with-example-aspect-advice-pointcut-joinpoint/"><em>Spring AOP Tutorial</em></a></b></li>
<li><b><a href="https://dineshonjava.com/using-spring-jdbc-framework-chapter-32/"><em>Spring JDBC Tutorial</em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-hateoas-hypermedia-driven-restful-web-service/"><em><strong>Spring HATEOAS </strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/microservices-with-spring-boot/"><em><strong>Microservices with Spring Boot</strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/jax-rs-web-service-tutorial/"><strong><em>REST Webservice</em> </strong></a></b></li>
<li><b><a href="https://dineshonjava.com/core-java-baby-step-to-be-best-java-ian/"><em><strong>Core Java </strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/hibernate-3-on-baby-steps/"><em><strong>Hibernate Tutorial</strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-batch-process-with-example/"><strong><em>Spring Batch</em> </strong></a></b></li>
</ul>
</div>
<h2><b>Difference between StringBuffer and StringBuilder in Java</b></h2>
<p>StringBuffer and StringBuilder classes are mutable version of String class. They all have common methods for string manipulation in spite of this they have difference in uses in java application. Before going to differences let&#8217;s discuss some words about both classes from java documents.</p>
<h3><b>StringBuffer class</b></h3>
<p>A thread-safe, mutable sequence of characters. A string buffer is like a String, but can be modified. At any point in time it contains some particular sequence of characters, but the length and content of the sequence can be changed through certain method calls.</p>
<p>String buffers are safe for use by multiple threads. The methods are synchronized where necessary so that all the operations on any particular instance behave as if they occur in some serial order that is consistent with the order of the method calls made by each of the individual threads involved.</p>
<pre class="highlight">public synchronized int length() { 
 return count; 
 } 
 
 public synchronized int capacity() { 
 return value.length; 
 } 
 
 
 public synchronized void ensureCapacity(int minimumCapacity) { 
 if (minimumCapacity >; value.length) { 
 expandCapacity(minimumCapacity); 
 } 
 } 
 
 /** 
 * @since 1.5 
 */ 
 public synchronized void trimToSize() { 
 super.trimToSize(); 
 } 
 
 /** 
 * @throws IndexOutOfBoundsException {@inheritDoc} 
 * @see #length() 
 */ 
 public synchronized void setLength(int newLength) { 
 super.setLength(newLength); 
 } 
 
 /** 
 * @throws IndexOutOfBoundsException {@inheritDoc} 
 * @see #length() 
 */ 
 public synchronized char charAt(int index) { 
 if ((index <; 0) || (index >;= count)) 
 throw new StringIndexOutOfBoundsException(index); 
 return value[index]; 
 } 
 
//Many more methods..... 
 
</pre>
<h3><b>StringBuilder class</b></h3>
<p>A mutable sequence of characters. This class provides an API compatible with StringBuffer, but with no guarantee of synchronization. This class is designed for use as a drop-in replacement for StringBuffer in places where the string buffer was being used by a single thread (as is generally the case). Where possible, it is recommended that this class be used in preference to StringBuffer as it will be faster under most implementations.</p>
<pre class="highlight">public StringBuilder append(Object obj) { 
 return append(String.valueOf(obj)); 
 } 
 
 public StringBuilder append(String str) { 
 super.append(str); 
 return this; 
 } 
 
 // Appends the specified string builder to this sequence. 
 private StringBuilder append(StringBuilder sb) { 
 if (sb == null) 
 return append("null"); 
 int len = sb.length(); 
 int newcount = count + len; 
 if (newcount >; value.length) 
 expandCapacity(newcount); 
 sb.getChars(0, len, value, count); 
 count = newcount; 
 return this; 
 } 
 
 /** 
 * @throws StringIndexOutOfBoundsException {@inheritDoc} 
 */ 
 public StringBuilder delete(int start, int end) { 
 super.delete(start, end); 
 return this; 
 } 
 
 /** 
 * @throws StringIndexOutOfBoundsException {@inheritDoc} 
 */ 
 public StringBuilder deleteCharAt(int index) { 
 super.deleteCharAt(index); 
 return this; 
 } 
 
 /** 
 * @throws StringIndexOutOfBoundsException {@inheritDoc} 
 */ 
 public StringBuilder replace(int start, int end, String str) { 
 super.replace(start, end, str); 
 return this; 
 } 
 
 /** 
 * @throws StringIndexOutOfBoundsException {@inheritDoc} 
 */ 
 public StringBuilder insert(int index, char str[], int offset, 
 int len) 
 { 
 super.insert(index, str, offset, len); 
 return this; 
 } 
 
//Many more methods..... 
 
</pre>
<h2><b>StringBuider vs StringBuffer</b></h2>
<p>There are following are main difference between StringBuffer and StringBuilder in Java</p>
<p><b>Synchronization</b><br />
All Methods in StringBuffer class are synchronized but StringBuilder is non synchronized. In short StringBuilder unsynchronized version of StringBuffer class. You can not share Instances of StringBuilder class between multiple threads. If such synchronization is required then it is better to use StringBuffer class.<br />
<b><br />
</b> <b>Performance</b><br />
Due thread-safe nature of StringBuffer make it slower than StringBuilder because of StringBuffer has overhead of acquiring and releasing locks associated with synchronized methods.</p>
<p><b>From which java version</b><br />
StringBuffer is available from JDK 1.0 but StringBuilder is available from JDK 1.5.</p>
<p><b>Which one to use and when</b><br />
StringBuffer and StringBuilder are almost same, both classes have same methods but StringBuffer has synchronized methods due to thread-safety. So that means if thread-safety is matter and performance is not matter for any operation then we should use StringBuffer instead of StringBuilder. There some areas in the application where performance is matter without thread safety then StringBuilder could be used. In short if you think that the operation should be thread-safe then use StringBuffer, in all other cases StringBuilder is a better choice as it offers you the same functionality with better performance.</p>
<p><b>Interested Point about String Concatenation</b><br />
Suppose you want concatenate two or more strings using + operator, Java internally convert that operator call to corresponding StringBuilder append() method. For example there are three string literals &#8220;Dinesh&#8221;, &#8221; on &#8221; , &#8220;Java&#8221; if want to add these three literal to make final string &#8220;Dinesh on Java&#8221; then we can concatenate these as &#8220;Dinesh&#8221; + &#8221; on &#8221; + &#8220;Java&#8221;, that will be converted to new StringBuilder().append(&#8220;Dinesh&#8221;).append(&#8221; on &#8220;).append(&#8220;Java&#8221;). Only problem is that it initialize StringBuilder with default capacity, which means expensive array copy operation, when StringBuilder get resized. So be careful whenever you want to concatenation of strings, in this case always use StringBuilder or StringBuffer.</p>
<h2><b>Summary</b></h2>
<p>In this article we have seen the basic features and some differences of StringBuffer and StringBuilder. StringBuffer and String class both are from Java first version but StringBuilder added from Java 5. StringBuffer and StringBuilder both classes are mutable unlike String is immutable. StringBuffer is thread-safe in nature i.e. all methods of this class are synchronized. But StringBuilder is simply unsynchronized copy of StringBuffer class.</p>
<div style="background-color: #f2f9fc; border-radius: 3px; border: 1px solid #c9e6f2; line-height: 1.45; padding: 16px;">
<p><b>String Related Posts</b></p>
<ol style="text-align: left;">
<li><b><a href="https://dineshonjava.com/how-to-make-java-class-immutable/">How to make Immutable Class in Java</a></b></li>
<li><b><a href="https://dineshonjava.com/core-java-interview-questions/">Core Java Interview Questions</a></b></li>
<li><b><a href="https://dineshonjava.com/string-class-in-java/">String Class in Java</a></b></li>
<li><b><a href="https://dineshonjava.com/why-string-is-immutable-and-final-in-java/">Why String is Immutable and Final in Java</a></b></li>
<li><b><a href="https://dineshonjava.com/convert-string-to-integer-to-string-in-java-with-example/">Convert String to Integer to String in Java with Example</a></b></li>
<li><b><a href="https://dineshonjava.com/convert-double-to-string-to-double-in-java-with-example/">Convert Double to String to Double in Java with Example</a></b></li>
<li><b><a href="https://dineshonjava.com/convert-date-to-string-in-java-with-example/">Convert Date to String in Java with Example</a></b></li>
<li><b><a href="https://dineshonjava.com/differences-between-string-stringbuffer-and-stringbuilder-in-java/">Differences between String, StringBuffer and StringBuilder in Java</a></b></li>
<li><b><a href="https://dineshonjava.com/difference-between-stringbuilder-and-stringbuffer-in-java/">Differences StringBuffer and StringBuilder</a></b></li>
<li><b><a href="https://dineshonjava.com/how-to-compare-two-strings-in-java/">How to Compare two Strings in Java</a></b></li>
<li><b><a href="https://dineshonjava.com/hashcode-and-equals-methods-in-java/">Difference between hashCode() and equals() methods in Java<br />
</a></b></li>
<li><b><a href="https://dineshonjava.com/java-string-substring-method/"> Java &#8211; String substring() Method<br />
</a></b></li>
<li><b><a href="https://dineshonjava.com/difference-between-wait-and-sleep/"> Difference between wait() and sleep()<br />
</a></b></li>
</ol>
</div>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/differences-between-string-stringbuffer-and-stringbuilder-in-java/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/how-to-compare-two-strings-in-java/">Next</a> 
									 </div> 
									</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
 $.post('https://dineshonjava.com/wp-admin/admin-ajax.php', {action: 'mts_view_count', id: '69'});
});
</script>
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…