Difference between String and StringBuffer in Java


It represents a sequence of characters in Java by using a character array. It has the following Syntex:
String string-name;
string-name=new String(“string”);
String sk;
sk=new String(“Hello World”);


StringBuffer is a peer class of String. While String creates strings of fixed_length, StringBuffer creates flexible length that can be modified in terms of both length and content. It is used to insert characters and sub-strings in the middle of a string or append another string to end.

s1.setChartAt(n, `x`)It modifies the nth character to x
s1.append(s2)It appends the string s2 to s1 at the end
s1.insert(n, s2)It inserts the string s2 at the position n of the string s1.
s1.setLength(n)It sets the length of the string s1. If n