Guys,why we use string buffer in java? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Guys,why we use string buffer in java?

14th Jul 2017, 4:30 PM
MANOJ KUMAR
MANOJ KUMAR - avatar
3 Answers
+ 5
string is immutable.so we can't perform some operation on strings,so overcome that problem we use string buffer it is mutable. am,I correct guys?
15th Jul 2017, 6:45 AM
MANOJ KUMAR
MANOJ KUMAR - avatar
+ 3
String buffer is mutable. Similar to why we might use arraylists instead of arrays, the string buffer is designed to be quick performance wise when adding (appending) to the string. So, if you ever feel the need to constantly change the value of a string, it may be better to use string buffer. As stated in many other sources, it's commonly used for String concatenation. Might be nice for large toStrings().
14th Jul 2017, 5:49 PM
Rrestoring faith
Rrestoring faith - avatar
+ 1
It's not that we can't, it's for performance. Example/ String a = "a"; a += "b"; This is creating a new string with the value of ab.
15th Jul 2017, 3:40 PM
Rrestoring faith
Rrestoring faith - avatar