1.Definition:
- 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:
- 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") ;
StringBuffer:
- By using its available constructors
- StringBuffer str= new StringBuffer();
StringBuilder:
- By using its available constructors
- StringBuilder str= new StringBuilder ();
3.Special operations those we can only perform on StringBuffer and StringBuilder:
- append
- insert
- delete
- reverse
- Since string is immutable we cannot perform these operations on String.
4.Concatenation:
- Using String object we can concat new string to the current string in two ways.
- Using + operator
- using concat() method.
- Using StringBuffer we can perform concat operation only in one way
- Using append() method
- Using StringBuilder we can perform concat operation only in one way
- Using append() method
package com.javaforus;
public Class SBDemo{
public static void main (String args[]) {
String str="Java";
StringBuffer sb= new StringBuffer("Java");
StringBuilder sbr= new StringBuilder("Java");
System.out.println(str.concat(" language"));
System.out.println(sb.append(" language"));
System.out.println(sbr.append(" language"));
}
}
OutPut:
Java language
Java language
Java language
5.Comparison:
- 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.