Javac string concatenation enhancement proposal

XMLWordPrintable

    • Type: Enhancement
    • Resolution: Not an Issue
    • Priority: P4
    • None
    • Affects Version/s: None
    • Component/s: tools
    • None

      A DESCRIPTION OF THE REQUEST :
      For string concatination str1 + str2 javac builds the following code

      (new StringBuilder(String.valueOf(str1))).append(str2).toString()

      I suggest a more efficient way

      new StringBuilder(str1.length() + str2.length()).append(str1).append(str2).toString()


      JUSTIFICATION :
      With the current implementation of string concatination StringBuilder may need to expand its buffer capacity each time a next string is appended, which is very inefficient. Creating a StringBuilder with a pre-calculated capacity ensures that this will never happens.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      for str1 + str2 javac builds this code

      new StringBuilder(str1.length() + str2.length()).append(str1).append(str2).toString()

      ACTUAL -
      for str1 + str2 javac builds this code

      (new StringBuilder(String.valueOf(str1))).append(str2).toString()

            Assignee:
            Vicente Arturo Romero Zaldivar
            Reporter:
            Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: