We know that once you declare a variable of type string, it points to an object, and when you modify the variable, the variable points to a new object. But like StringBuffer, it doesn't produce new objects:
StringBuffer sb = new StringBuffer ();
Sb.append ("abc"). Append ("Def");
Here SB will always point to the same object. And we use "+" to stitch up two strings, such as String str = STR1+STR2 do the following:
Call string.valueof (obj), where obj is a String-type variable, as the str1 and str2,string.valueof () mentioned above are: return OBJ = null? Null ": obj.tostring (); generates StringBuilder, invokes StringBuilder (STR1) Construction method, initializes StringBuilder, Str1.length+16, and invokes append (str1); Continue to invoke append (STR2); finally call Stringbuilder.tostring () return the result so the final conclusion should be: Stringbuilder.append (str1). Append (str2). ToString ().
If there are any errors, please also point out that learn from each other.
This article refers to: http://blog.csdn.net/zolalad/article/details/30089327