Java Cracker

-by Kausik Jana

StringBuffer and StringBuilder in Java

StringBuffer:

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.

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.

The principal operations on a StringBuffer are the append and insert methods, which are overloaded so as to accept data of any type. Each effectively converts a given datum to a string and then appends or inserts the characters of that string to the string buffer. The append method always adds these characters at the end of the buffer; the insert method adds the characters at a specified point.

For example, if z refers to a string buffer object whose current contents are “start”, then the method call z.append("le") would cause the string buffer to contain “startle”, whereas z.insert(4, "le") would alter the string buffer to contain “starlet”.

In general, if sb refers to an instance of a StringBuffer, then sb.append(x) has the same effect as sb.insert(sb.length(), x).

 StringBuilder:

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.

The principal operations on a StringBuilder are the append and insert methods, which are overloaded so as to accept data of any type. Each effectively converts a given datum to a string and then appends or inserts the characters of that string to the string builder. The append method always adds these characters at the end of the builder; the insert method adds the characters at a specified point.

For example, if z refers to a string builder object whose current contents are “start”, then the method call z.append(“le”) would cause the string builder to contain “startle”, whereas z.insert(4, “le”) would alter the string builder to contain “starlet”.

In general, if sb refers to an instance of a StringBuilder, then sb.append(x) has the same effect as sb.insert(sb.length(), x). Every string builder has a capacity. As long as the length of the character sequence contained in the string builder does not exceed the capacity, it is not necessary to allocate a new internal buffer. If the internal buffer overflows, it is automatically made larger.

Instances of StringBuilder are not safe for use by multiple threads. If such synchronization is required then it is recommended that StringBuffer be used.
 (from  java doc) 

 Difference between StringBuffer and StringBuilder: 

1. StringBuffer and StringBuilder have almost same features.
2. Methods of the StringBuffer are synchronized where methods of the StringBuilder are not synchronized
3. StringBuilder is faster than StringBuffer

Let’s take an example of this scenario :

 Java |  copy code |? 
01
package com.foo.test; 
02
 
03
/** 
04
 * @author Kaushik.Jana 
05
 *  
06
 */ 
07
public class StringBufferAndStringBuilderTest { 
08
 
09
public static void main(String[] args) { 
10
 
11
StringBuffer sBuffer = null; 
12
StringBuilder sBuilder = null; 
13
int n = 99999999; 
14
long t = 0L; 
15
 
16
{ 
17
sBuffer = new StringBuffer(); 
18
t = System.currentTimeMillis(); 
19
for (int i = 0; i++ < n;) 
20
sBuffer.append(""); 
21
System.out.println("Time taken by StringBuffer: " 
22
+ (System.currentTimeMillis() - t)); 
23
} 
24
 
25
{ 
26
sBuilder = new StringBuilder(); 
27
t = System.currentTimeMillis(); 
28
for (int i = 0; i++ < n;) 
29
sBuilder.append(""); 
30
System.out.println("Time taken by StringBuilder: " 
31
+ (System.currentTimeMillis() - t)); 
32
} 
33
 
34
} 
35
} 
36

Result :

Time taken by StringBuffer: 8267
Time taken by StringBuilder: 512

2 Comments

  1. Nice post on String buffer and String builder with Example

  2. Nice one :) Just a mere reading is enough to Understand the concept :)

Leave a Reply

Your email address will not be published.

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

© 2014 Java Cracker

Proudly Powered by Wordpress