String objects are immutable, meaning once an String object is created, it cannot be changed. Any modification results in the creation of a new
String object. String is inherently thread-safe because of its immutability.
Due to immutability, concatenating strings String can be less efficient because it involves creating new objects and copying existing content. Used when the value of the string will not change or will change infrequently.
String str = "Hello";
str = str.concat(", World!"); // A new String object is created
StringBuilder
StringBuilder objects are mutable, meaning they can be modified after they are created without creating new objects.
StringBuilder is not thread-safe, which means it is not synchronized and should not be used in a multi-threaded environment.
StringBuilder sb = new StringBuilder("Hello");
sb.append(", World!"); // Modifies the existing object
Generally faster than StringBuffer because it does not have the overhead of synchronization. It is also more efficient than
String for concatenation operations due to mutability. Used when you need a mutable sequence of characters and are working in a single-threaded environment.
StringBuffer
StringBuffer objects are mutable, similar to StringBuilder.
StringBuffer is thread-safe because its methods are synchronized, which makes it safe to use in a multi-threaded environment.
Slower than StringBuilder due to the overhead of synchronization but still more efficient than
String for concatenation operations in a multi-threaded context. Used when you need a mutable sequence of characters in a multi-threaded environment.
StringBuffer sb = new StringBuffer("Hello");
sb.append(", World!"); // Modifies the existing object
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
String
Stringobjects are immutable, meaning once anStringobject is created, it cannot be changed. Any modification results in the creation of a newStringobject.Stringis inherently thread-safe because of its immutability.Due to immutability, concatenating strings
Stringcan be less efficient because it involves creating new objects and copying existing content. Used when the value of the string will not change or will change infrequently.StringBuilder
StringBuilderobjects are mutable, meaning they can be modified after they are created without creating new objects.StringBuilderis not thread-safe, which means it is not synchronized and should not be used in a multi-threaded environment.Generally faster than
StringBufferbecause it does not have the overhead of synchronization. It is also more efficient thanStringfor concatenation operations due to mutability. Used when you need a mutable sequence of characters and are working in a single-threaded environment.StringBuffer
StringBufferobjects are mutable, similar toStringBuilder.StringBufferis thread-safe because its methods are synchronized, which makes it safe to use in a multi-threaded environment.Slower than
StringBuilderdue to the overhead of synchronization but still more efficient thanStringfor concatenation operations in a multi-threaded context. Used when you need a mutable sequence of characters in a multi-threaded environment.Key Differences
StringBuilderdue to synchronizationRead more
How do you implement a singleton pattern in Java?
What is the significance of the final keyword when applied to classes, methods, and variables?