Duplicate :
|
|
Duplicate :
|
|
Relates :
|
The optimization is to have the (JIT) compiler identify StringBuilder and StringBuffer usages where a StringBuilder or StringBuffer is allocated, things are appended to the StringBuilder or StringBuffer and StringBuilder.toString() or StringBuffer.toString() is called. Usages where the StringBuilder or StringBuffer does not escape or becomes dead, the char[] allocated by the StringBuilder or StringBuffer could be assigned to the String returned by the toString() method of StringBuilder or StringBuffer instead of allocating an additional char[] in a new String and copying the char's from the StringBuilder or StringBuffer's char[] to the String's newly allocated char[]. The end result is the saving of an unnecessary char[] allocation and a subsequent call to arraycopy.