Most of the problems with using String stem from the fact that String objects are immutable. Once they've
been created, they cannot be changed. Operations that might appear to modify String objects actually generate completely new ones.
Then what happen with string literal ?
The String and StringBuffer classes are meant to be used together to overcome this situation.
Example:
String xyz = "x" + y + "z";
It automatically transforms the code to
String xyz = new StringBuffer().append("x")
.append(y)
.append("z")
.toString();
This gives you an idea how String concatenation actually works. Note that two objects are created to perform the transformation: A new StringBuffer is created explicitly and a new String is returned from toString.
been created, they cannot be changed. Operations that might appear to modify String objects actually generate completely new ones.
Then what happen with string literal ?
The String and StringBuffer classes are meant to be used together to overcome this situation.
Example:
String xyz = "x" + y + "z";
It automatically transforms the code to
String xyz = new StringBuffer().append("x")
.append(y)
.append("z")
.toString();
This gives you an idea how String concatenation actually works. Note that two objects are created to perform the transformation: A new StringBuffer is created explicitly and a new String is returned from toString.
0 comments:
Post a Comment