title: How does the concatenation of a String with characters work in Java?
body: <p>The following is a problem from codingbat.</p>
  
  <blockquote>
    <p>Given a string, return a string where for every char in the original, there are two chars.
    e.g.:</p>
  
  <pre><code>doubleChar("The") → "TThhee"
  doubleChar("AAbb") → "AAAAbbbb"
  doubleChar("Hi-There") → "HHii--TThheerree"
  </code></pre>
  </blockquote>
  
  <p>I have two statements that can do this, but the statement in the comment doesn't give the excepted output:</p>
  
  <pre><code>public String doubleChar(String str) {
      String str1 = "";
      for(int i=0;i&lt;str.length();i++)
      {
          //str1 += str.charAt(i) + str.charAt(i);
          str1 += str.substring(i,i+1)+str.substring(i,i+1);
      }
      return str1;
  }
  </code></pre>
  
  <p>If I change the commented part to <code>str1 = str1 + str.charAt(i) + str.charAt(i)</code>, the output is as required. I am not able to understand this. If the concatenation doesn't, then it shouldn't work for either of the case. Can you help me in this?</p>