Discuss the difference between str str word string concate
Discuss the difference between
str = str + word; //string concatenation
and
tempStringBuffer.append(word);
where str is a String object and tempStringBuffer is a StringBuffer object.
#######################################################################
Solution
Difference between str = str + word; //string concatenation and tempStringBuffer.append(word);
Answer:
Both will give the same result. Both statements are used to contact the two strings.
But when we use this statement \'str = str + word; \" to concatenate the strings, internally two string objects will be created and that will be concatenated and assigned the result back to str String object.
Becuase String are immutables and value can not be changed once assigned to String object. So if we want to concatenate the another string with this string we have to use either \'+\' or concate method available in String class.
Where as
StringBuffer is a mutable class. Value can be changed at runtime. So we can operate any operation on that object means we can change that value.
tempStringBuffer.append(word);
When we use this statement, No other objects will be created internally other than tempStringBuffer object.

