Several methods in StringBuilder and StringBuffer return String objects. Their specifications require creation of new String objects even though this is unnecessary in some circumstances. Such methods include:
substring(int)
substring(int, int)
subSequence(int, int)
toString()
in both StringBuffer and StringBuilder.
The specs of the corresponding methods in String were clarified byJDK-7174936, and the spec of the CharSequence.subSequence method was clarified by JDK-8028757.
An example where a new String object need not be created might be if the result of calling toString() were cached, so that subsequent calls to toString() might return the same String. Another example would be returning "" (the empty string literal) if a substring has zero length.
substring(int)
substring(int, int)
subSequence(int, int)
toString()
in both StringBuffer and StringBuilder.
The specs of the corresponding methods in String were clarified by
An example where a new String object need not be created might be if the result of calling toString() were cached, so that subsequent calls to toString() might return the same String. Another example would be returning "" (the empty string literal) if a substring has zero length.
- relates to
-
JDK-7174936 (spec str) several String methods claim to always create new String
- Closed
-
JDK-8028757 (spec str) CharSequence.subSequence improperly requires a "new" CharSequence be returned
- Closed
-
JDK-8187652 (spec str) clarify specs of String.replace, replaceFirst, replaceAll
- Open