I'm sure this issue was answered many times on many different websites many years ago. Still, I have some reservations, so I decided to share this. The fundamental distinction is that String is immutable, and each operation on String results in the creation of a new String Object.
Ex:-
Instead of changing the old str, two new Strings are produced in the preceding scenario, which may be prevented by utilising StringBuffer.
Ex:
My concern is, why are we sending a String to the add procedure again? In the above scenario, no new String objects are produced for "try" and "this" in the String pool.
Ex:-
Java:
String str = "some";
str = str + " text"
Instead of changing the old str, two new Strings are produced in the preceding scenario, which may be prevented by utilising StringBuffer.
Ex:
Java:
StringBuffer str = new StringBuffer();
str.append("try");
str.append("this");
My concern is, why are we sending a String to the add procedure again? In the above scenario, no new String objects are produced for "try" and "this" in the String pool.