It is a predefined class in java.lang package can be used to handle the String. StringBuilder class is almost similar to to StringBuffer class. It is also a mutable object.
The main difference StringBuffer and StringBuilder class is StringBuffer is thread safe that means only one threads allowed at a time to work on the String where as StringBuilder is not thread safe that means multiple threads can work on same String value.
Difference between StringBuffer and StringBuilder
All the things between StringBuffer and StringBuilder are same only difference is StringBuffer is synchronized and StringBuilder is not synchronized. synchronized means one thread is allow at a time so it thread safe. Not synchronized means multiple threads are allow at a time so it not thread safe.
|1||It is thread safe.||It is not thread safe.|
|2||Its methods are synchronized and provide thread safety.||Its methods are not synchronized and unable to provide thread safety.|
|3||Relatively performance is low because thread need to wait until previous process is complete.||Relatively performance is high because no need to wait any thread it allows multiple thread at a time.|
|1||Introduced in 1.0 version.||Introduced in 1.5 version.|
When we use String, StringBuffer and StringBuilder
- If the content is fixed and would not change frequently then we use String.
- If content is not fixed and keep on changing but thread safety is required then we use StringBuffer
- If content is not fixed and keep on changing and thread safety is not required then we use StringBuilder