• String is an immutable sequence of characters.
  • StringBuffer is mutable sequence of characters.
  • StringBuilder is also mutable sequence of characters.

The only difference between StringBuffer and StringBuilder: 

  • StringBuffer object is thread safe , it means StringBuffer object is modified by multiple concurrently, because  all its methods are declared as "synchronized".
  • StringBuilder class is given in jdk 1.5 version as non thread -safe class, means all its methods are non synchronized methods.
  • So , in single model application we must use StringBuilder, so that object locking and unlocking will not be there, hence performance is increased.
  • In single thread model application operations are executed in sequence hence there is no chance of object corruption.

When should we choose String and StringBuffer? 

  • If we do not want to store string modifications in the same memory we must choose String.
  • To do modifications in the same memory, we must choose StringBuffer or StringBuilder.

 Advantage and disadvantage in String: 

  • Advantage : Since modifications are preserving in another memory location, we will have both original and modified values.
  • Disadvantage: It consumes lot memory for every operation, as it stores it modifications in new memory. So it leads to performance issue.
  • Solution: To solve this performance issue , in projects developers store string data using StringBuilder or StringBuffer after all modifications they convert into String and pass it back to user.

Advantage and disadvantage of StringBuffer or StringBuilder:

  • Advantage: It given high performance because consumes less memory as all modifications stored in same memory.
  • Disadvantage: Original value will not be preserved.

2.Creating String , StringBuffer objects: 


  • String object can be created in two ways.
  • By using string literal : String str="instance of java ";
  • By using its constructors: String str= new String("instaneofjavaforus") ;


  • By using its available constructors
  • StringBuffer str= new StringBuffer();


  • By using its available constructors
  • StringBuilder str= new StringBuilder ();

3.Special operations those we can only perform on StringBuffer and StringBuilder: 

  1. append
  2. insert
  3. delete
  4. reverse
  •  Since string is immutable we cannot perform these operations on String.


  • Using String object we can concat new string to the current  string in two ways.
  1. Using + operator
  2. using concat() method.
  • Using StringBuffer we can perform concat operation only in one way
  1. Using append() method
  • Using StringBuilder we can perform concat operation only in one way
  1. Using append() method

  1. package com.instanceofjavaforus;
  2. public Class SBDemo{ 
  4. public static void main (String args[]) {
  6.         String str="Java";
  7.         StringBuffer sb= new StringBuffer("Java");
  8.         StringBuilder sbr= new StringBuilder("Java");
  10.        System.out.println(str.concat(" language"));    
  11.        System.out.println(sb.append(" language"));
  12.         System.out.println(sbr.append(" language"));
  13. }
  14. }
  16. OutPut:
  17. Java language
  18. Java language
  19. Java language


  • Using equals() method String objects are compared with state, because it is overridden in this class. Also hashcode(0 method is overridden in order to satisfy equals() method contract.
  • But in StringBuffer and in StringBuilder equals() method is not overridden , so using equals() method their object are compared with reference. 
You might like:

Instance Of Java

We will help you in learning.Please leave your comments and suggestions in comment section. if you any doubts please use search box provided right side. Search there for answers thank you.
Newer Post
Older Post

1 comments for String vs stringbuffer vs stringbuilder

  1. It's also worthwhile to note that if you write an expression using "+" to concatenate strings, the resulting bytecode just uses StringBuilder and "append" methods. On a single line, concatenating string portions with "+" is just as efficient as using StringBuilder, because the code is identical.


Select Menu