Wednesday, December 7, 2011

Difference between String, StringBuffer and StringBuilder in Java

String is most commonly used class in Java programming. Java also provides two other classes StringBuffer and StringBuilder. We need to carefully choose between these three depending on the requirement. Thus it is important to understand difference among them.

Points of Difference

1. String class objects are immutable objects means that they can’t be altered. It always creates a new object when we try to change them. However StringBuffer and StringBuilder classes are mutable and one can change the contents of it.


To concatenate String s1="ABC" and String s2="DEF" using "+" operator we need another string. String s3 = s1+s2 and s3 will have the value of "ABCDEF".

But using StringBuffer or StringBuilder the same scenario in this case can be s1.append("DEF") and s1 becomes "ABCDEF". They are provided by append() and insert() methods.

2.  When we use the String object to concatenate two strings, the first string is combined to the other string by creating a new copy in the memory as a string object because they are immutable objects. This process is a little long.

StringBuffer / StringBuilder are mutable, doesn’t create new objects every time. Thus usage of StringBuilder / StringBuffer is more efficient in case large amounts of string manipulations have to be performed.

3. The String class overrides the default equals() method, but StringBuffer and StringBuilder do not override the default equals() method in Object class. The equals() method for class Object implements the most discriminating possible equivalence relation on objects that is, for any non-null reference values x and y, this method returns true if and only if x and y refer to the same object (x == y has the value true).


public class Program {
            public static void main(String[] args) {
                String s1 = new String("Hello World");
                String s2 = new String("Hello World");
                System.out.println("s1.equals(s2):" + s1.equals(s2));
                StringBuffer sb1 = new StringBuffer("Hello World");
                StringBuffer sb2 = new StringBuffer("Hello World");
                System.out.println("sb1.equals(sb2):" + sb1.equals(sb2));
                StringBuilder sd1 = new StringBuilder("Hello World");
                StringBuilder sd2 = new StringBuilder("Hello World");
                System.out.println("sd1.equals(sd2):" + sd1.equals(sd2));

The output is

4. StringBuffer and StringBuilder have the same methods with one difference and that’s of synchronization. StringBuffer is synchronized (which means it is thread safe and hence you can use it when you implement threads for your methods) whereas StringBuilder is not synchronized (which implies it isn’t thread safe).

5. It is recommended to use StringBuilder over StringBuffer whenever possible because it is faster than StringBuffer. However if thread safety is necessary the best option is StringBuffer objects.

6. StringBuilder class is relatively new and was introduced in Java 1.5 whereas String and StringBuffer are old classes. StringBuffer and String class was introduced in JDK 1.0

Criteria to choose among String, StringBuffer and StringBuilder

1. If your text is not going to change use a string Class because a String object is immutable.

2. If your text can change and will only be accessed from a single thread, use a StringBuilder because StringBuilder is unsynchronized.

3. If your text can change and will be accessed from multiple threads, use a StringBuffer because StringBuffer is synchronous.        

5 Responses to “Difference between String, StringBuffer and StringBuilder in Java”

j2eethoughts said...
December 9, 2011 at 2:25 PM

Why are you creating a new String object?
String s1 = new String("Hello World");
String s2 = new String("Hello World");

Instead of this, you could create the String this way:
String s1 = "Hello World";
String s2 = "Hello World";

This gives the opportunity to the JVM to reuse the same String literal and save some memory.


Anonymous said...
December 9, 2011 at 3:56 PM

Nice Post..Helped me so much..thanks

Sandeep Kumar said...
December 9, 2011 at 10:41 PM

Thanks for your suggestion Josep.

Javin @ String Replace Example said...
December 12, 2011 at 2:02 PM

Nice post , By the way I have also blogged on this as StringBuffer vs String vs StringBUilder in Java let me know how do you find it.

extremejava said...
December 23, 2011 at 12:02 PM

StringBuffer and StringBuilder both have common super class named AbstractStringBuilder.Java Split String

Post a Comment

© 2011 • All rights reserved.
Blogger Template by Bloggermint