Monday, 22 August 2016

String Vs StringBuffer Vs StringBuilder in Java

String in Java has many characteristics like being immutable, thread safety, declared as final that sets it apart. But, being immutable also means that any modification to String object will result in a creation of new string even if you use operations like converting to lowercase, uppercase. Thus concatenation of many strings will result in creation of many objects making it less memory efficient in that case.

Enters StringBuffer which is mutable and has an append() method which can append to the original StringBuffer and doesn’t result in creation of many objects. StringBuffer is also thread safe but here thread safety comes from the fact that all of its methods are synchronized. This synchronization makes it a little slow.

Enters StringBuilder class which provides the same methods as StringBuffer but it is not thread safe. Which makes it faster, as it performs no synchronization.

In this post we’ll go through these difference among the String, StringBuffer and StringBuilder classes which is a very important String interview question too.

Difference among String, StringBuffer and StringBuilder

String StringBuffer StringBuilder
Creation Apart from creating strings using new operator, String object can also be created like primitive data types are created as example String str = "test"; When String is created this way it is stored in a common string pool. StringBuffer object can only be created using new operator. StringBuffer object is stored in Heap. StringBuilder object can only be created using new operator. StringBuilder object is stored in Heap.
Modifiability Once you create a String object the content of that string cannot be modified i.e. String objects are immutable. Here being immutable means whenever you perform any operation on string which alters its content a new string object is created which contains the modified string. Original string is left as it is. StringBuffer is a mutable(modifiable) sequence of characters. Thus in case of StringBuffer length and content of the sequence can be changed through certain method calls. StringBuilder is a mutable(modifiable) sequence of characters. Thus in case of StringBuilder length and content of the sequence can be changed through certain method calls.
Appending If you have to append a lot of strings it will take more memory. Since string is immutable so concatenation of strings will result in creation of a lot of intermediary strings. StringBuffer class is mutable so appending to it using append() method will not result in creation of intermediary objects. That way less memory is used. StringBuilder class is mutable so appending to it using append() method will not result in creation of intermediary objects. That way less memory is used.
Thread-safety As string objects are immutable so they are thread safe. StringBuffer is thread safe so String buffers are safe for use by multiple threads. Each method in StringBuffer is synchronized making it thread safe. StringBuilder is not thread-safe.
Performance String class is fast. All the methods in StringBuffer class are synchronized which makes it a little slow. If you don't have to bother about thread safety then use StringBuilder class as it supports all of the same operations as StringBuffer but it is faster, as it performs no synchronization.

That's all for this topic String Vs StringBuffer Vs StringBuilder in Java. If you have any doubt or any suggestions to make please drop a comment. Thanks!


Related Topics

  1. String charAt() and subString() methods in Java
  2. Searching within a String using indexOf(), lastIndexOf() and contains() methods
  3. Splitting a String using split() method in Java
  4. String join() method in Java 8
  5. Java String interview questions

You may also like -

>>>Go to Java Basics page

1 comment:

  1. this is really too useful and have more ideas from yours. keep sharing many techniques. eagerly waiting for your new blog and useful information. keep doing more.
    Sharepoint Training in Chennai

    ReplyDelete