To check StringBuffer vs StringBuilder we must check AbstractStringBuffer first coz both StringBuffer and StringBuilder extends AbstractStringBuilder.
AbstractStringBuilder has 'char value' that is used for holding string in from of char sequence. 'int count' is used for storing number of characters stored.
Important methods in AbstractStringBuilder:
- capacity(): Capacity is amount of store available for newly inserted characters
- insert(int, <T>): insert <T> into character sequence at int.
- reverse(): Reverse of the character sequence
- append(): Append to the character sequence
- toString(): Only abstract method in AbstractStringBuilder
getValue(): This is a final method that returns char value;
In simple words all operations are done on array of characters which is present in AbstractStringBuilder.
Now StringBuffer vs StringBuilder
- StringBuffer and StringBuilder extends AbastractStringBuilder
- Both use AbastractStringBuilder for logical operation and has minimal code written in them
- Both implements toString() where they create new String and do not share char value with String.
- Both implements writeObject() & readObject()
Now the major and important difference is all important methods in StringBuffer are synchronized. Due to non synchronized methonds StringBuilder performs better than StringBuffer.