title: String concatenation with Groovy
body: <p>What is the best (idiomatic) way to concatenate Strings in Groovy?</p>
  
  <p>Option 1:</p>
  
  <pre><code>calculateAccountNumber(bank, branch, checkDigit, account) {
      bank + branch + checkDigit + account
  }
  </code></pre>
  
  <p>Option 2:</p>
  
  <pre><code>calculateAccountNumber(bank, branch, checkDigit, account) {
      "$bank$branch$checkDigit$account"
  }
  </code></pre>
  
  <p>I've founded an interesting point about this topic in the old Groovy website: Things you can do but better leave undone.</p>
  
  <blockquote>
    <p>As in Java, you can concatenate Strings with the "+" symbol. But Java
    only needs that one of the two items of a "+" expression to be a
    String, no matter if it's in the first place or in the last one. Java
    will use the toString() method in the non-String object of your "+"
    expression. But in Groovy, you just should be safe the first item of
    your "+" expression implements the plus() method in the right way,
    because Groovy will search and use it. In Groovy GDK, only the Number
    and String/StringBuffer/Character classes have the plus() method
    implemented to concatenate strings. To avoid surprises, always use
    GStrings.</p>
  </blockquote>