Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8138614

(spec str) StringBuffer and StringBuilder methods improperly require "new" String to be returned

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P4 P4
    • 25
    • 8, 9
    • core-libs
    • b12

      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 by JDK-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.

      The subSequence() method is declared to return CharSequence, but in both StringBuilder and StringBuffer it is specified to behave identically to the substring() method, which returns a String. Thus subSequence() returns a String that has been upcast to CharSequence. Since String objects can be freely reused, the relaxation of the "new" requirement does apply to subSequence(). (It doesn't apply to CharBuffer, though, which specifically requires that it returns a new CharBuffer.)

        There are no Sub-Tasks for this issue.

            smarks Stuart Marks
            smarks Stuart Marks
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: