title: Why does Groovy replace java.lang.String.length() with size()?
body: <p>Wikipedia's <a href="https://en.wikipedia.org/w/index.php?title=Groovy_%28programming_language%29&amp;oldid=667478169" rel="noreferrer">current article</a> about the Groovy programming language explains that "Most valid Java files are also valid Groovy files" and gives the following examples, first of Java code:</p>
  
  <pre><code>for (String it : new String[] {"Rod", "Carlos", "Chris"})
      if (it.length() &lt;= 4)
          System.out.println(it);
  </code></pre>
  
  <p>Then the same in Groovy:</p>
  
  <pre><code>["Rod", "Carlos", "Chris"].findAll{it.size() &lt;= 4}.each{println it}
  </code></pre>
  
  <p>Notice in the first example that we used the perfectly ordinary Java method, <a href="https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#length--" rel="noreferrer">java.lang.String.length()</a>. In the second example this method has been mysteriously replaced with a call to a method called <code>size()</code>. I have <a href="https://groovyconsole.appspot.com/" rel="noreferrer">verified</a> that the second example is valid Groovy code and has the correct behaviour.</p>
  
  <p><code>java.lang.String</code> does not have a method called <code>size()</code>. Groovy does not subclass <code>String</code> for its own purposes:</p>
  
  <pre><code>String s = ""
  Class c = s.getClass()
  println c.getName() // "java.lang.String"
  </code></pre>
  
  <p>nor does it somehow add extra methods to the <code>String</code> object:</p>
  
  <pre><code>// [...]
  for (def method : c.getMethods()) {
      println method.getName()
  }
  // prints a whole bunch of method names, no "size"
  </code></pre>
  
  <p>and yet still this code somehow works:</p>
  
  <pre><code>// [...]
  println s.size() // "0"
  </code></pre>
  
  <p>I can't find any Groovy documentation to explain this.</p>
  
  <ul>
  <li>Where does <code>size()</code> come from?</li>
  <li>Why does it not appear on the object?</li>
  <li>Why was it added?</li>
  <li>What was wrong with <code>length()</code> and why is it not preferred?</li>
  <li>What other extra methods have been added to <code>java.lang.String</code>?</li>
  <li>What about other standard classes?</li>
  </ul>