StringBuilder and StringBuffer:

--> Using string builder we can modify the string in the same location. Note: When we are modifying that string it will not generate one more object. --> String builders will be stored in the heap.

Creating and using string builder:

StringBulider
StringBulider1
Note: When we use '==' operator on strings it will check for the address equality and not for the content. if(t.equals(v)) //now the o/p will be printed. { System.out.println("t & v are same"); } StringBulider2

Advantages of string builder:

--> We can modify the content of a string builder in the same location. --> I want to create a string and share it with multiple threads and all the threads should be able to modify that string. StringBulider3

Synchronization:

When multiple threads are trying to modify same memory location inorder to avoid memory corruptions O.S will use some locking techniques.It is called as "synchronization". Note: String builders are not meant for synchronized used cases. --> We cannot use string builder with multiple threads because locking mechanism is not implemented in the string builder class. Conclusion: Do not use string builder with multiple threads.

String buffer:

It is same as string buffer but with extra locking techniques.

Properties:

--> String buffers will be created in heap memory. --> String buffers are 'mutable', --> String buffer has synchronization or locking techniques. --> We can use string buffers with multi-threads. Note: We can use same functions of string builder on string buffer also.
class Test
{
  public static void main(String[ ] args)
  {
    StringBuffer sb=new StringBuffer("hi");
  }
}
Note: Locking and unlocking is time taking process which is almost equivalent to 100 assembly level instructions. --> Do not use locking and unlocking if it is not required. 1. I want to create a string which is non-modifiable and I don't want to share it with other threads.[use string class]. 2. I want to create a mutable string and I don't want to share it with other threads[use string builder because it is faster than string buffer]. Don't use char array unless it is required. 3. I want to create a string and share it with multiple threads where nobody should be able to modify it.[use string class]. 4. I want to create a string and share with multiple threads where everyone can modify it.[Use string buffer]. Note: Do not use char array unless it is required. Instead use string builder because string builder will have lot of utility functions like append, insert, delete but char. array do not have all these.