Where to use a StringBuffer/StringBuilder than a String in Java?
- The String class objects are immutable whereas the StringBuffer and the StringBuilder objects are mutable.
- A StringBuffer is synchronized while a StringBuilder is not synchronized.
- A Concatenation operator "+" is internally implemented using either StringBuffer or StringBuilder.
- If the Object value is not going to change use String Class because a String object is immutable.
- If the Object value can change and will only be accessed from a single thread, use a StringBuilder because StringBuilder is unsynchronized.
- If the Object value can change and will be modified by multiple threads, use a StringBuffer because StringBuffer is synchronized.
Published on 28-May-2019 15:07:53