title: String concatenation performance in Log4j
body: <p>I have often heard people saying that its one of the best practices to avoid <code>String Concatenation</code> and use <code>{}</code> instead while logging. </p>
  
  <p>I was looking into the <code>Log4j</code> code to see how they have handled this and figured that they are doing something similar.</p>
  
  <p>Here is a snippet of <code>format()</code> method which takes the pattern and arguments and returns the message to be logged.</p>
  
  <pre><code>/**
   * Formats arguments using SLF4J-like formatter.
   * @param pattern pattern, may be malformed.
   * @param arguments arguments.
   * @return Message string
   */
  private static String format(final String pattern,
                               final Object[] arguments) {
      if (pattern != null) {
          String retval = "";
          int count = 0;
          int prev = 0;
          int pos = pattern.indexOf("{");
          while(pos &gt;= 0) {
              if (pos == 0 || pattern.charAt(pos-1) != '\\') {
                  retval += pattern.substring(prev, pos);
                  if (pos + 1 &lt; pattern.length() &amp;&amp; pattern.charAt(pos+1) == '}') {
                      if(arguments != null &amp;&amp; count &lt; arguments.length) {
                          retval += arguments[count++];
                      } else {
                          retval += "{}";
                      }
                      prev = pos + 2;
                  } else {
                      retval += "{";
                      prev = pos + 1;
                  }
              } else {
                  retval += pattern.substring(prev, pos - 1) + "{";
                  prev = pos + 1;
              }
              pos = pattern.indexOf("{", prev);
          }
          return retval + pattern.substring(prev);
      }
      return null;
  }
  </code></pre>
  
  <p>I'm not able to understand how this implementation is better than using concatenation. Any insights into this will be really helpful.</p>