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

Optimize String Concat (Unroll) at Compile Time

XMLWordPrintable

    • generic
    • generic

      A DESCRIPTION OF THE PROBLEM :
      The following are equivalent and should be compiled to the same byte code. I see this often in projects, developers use the String concatenation operator "+" inside the body of a StringBuilder/StringBuffer append method. When this is the case, the operator should be converted into an 'append' call on the parent StringBuilder/StringBuffer instead of creating a new StringBuilder/StringBuffer for the intermediate results.

        public static void main(String[] args) {
          StringBuilder sb = new StringBuilder();
          sb.append(args[0] + ' ' + args[1]);
          System.out.println(sb.toString());
        }

        public static void main(String[] args) {
          StringBuilder sb = new StringBuilder();
          sb.append(args[0]);
          sb.append(' ');
          sb.append(args[1]);
          System.out.println(sb.toString());
        }


            redestad Claes Redestad
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated: