12

Wikipedia's current article 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:

for (String it : new String[] {"Rod", "Carlos", "Chris"})
    if (it.length() <= 4)
        System.out.println(it);

Then the same in Groovy:

["Rod", "Carlos", "Chris"].findAll{it.size() <= 4}.each{println it}

Notice in the first example that we used the perfectly ordinary Java method, java.lang.String.length(). In the second example this method has been mysteriously replaced with a call to a method called size(). I have verified that the second example is valid Groovy code and has the correct behaviour.

java.lang.String does not have a method called size(). Groovy does not subclass String for its own purposes:

String s = ""
Class c = s.getClass()
println c.getName() // "java.lang.String"

nor does it somehow add extra methods to the String object:

// [...]
for (def method : c.getMethods()) {
    println method.getName()
}
// prints a whole bunch of method names, no "size"

and yet still this code somehow works:

// [...]
println s.size() // "0"

I can't find any Groovy documentation to explain this.

  • Where does size() come from?
  • Why does it not appear on the object?
  • Why was it added?
  • What was wrong with length() and why is it not preferred?
  • What other extra methods have been added to java.lang.String?
  • What about other standard classes?
qntm
  • 4,147
  • 4
  • 27
  • 41
  • Just a quick guess: Groovy might be using it's own [metaprogramming](https://en.wikipedia.org/wiki/Groovy_%28programming_language%29#Metaprogramming) for magic like this. – JimmyB Jul 15 '15 at 15:15
  • And [this](http://docs.groovy-lang.org/latest/html/api/org/codehaus/groovy/runtime/StringGroovyMethods.html#size%28java.lang.String%29) is where it comes from apparently. [Here](http://docs.groovy-lang.org/latest/html/api/org/codehaus/groovy/runtime/DefaultGroovyMethodsSupport.html) you can see what other classes/methods are there. – JimmyB Jul 15 '15 at 15:19
  • 1
    "What was wrong with length()?" - I always felt that `length()` was not really consistent (much less `Array.length`!) while the collections API uses `size()`. – JimmyB Jul 15 '15 at 15:45
  • 1
    And it doesn't _"replace"_ length with size (as alluded to in the question), it adds a new `size` method, and `length` is still there – tim_yates Jul 15 '15 at 15:51
  • I assumed that `length()` had been replaced, because it doesn't make sense to have two methods which do exactly the same thing. – qntm Jul 15 '15 at 17:42
  • Apart from you could have a function which accepts a strong or a collection and does something based on its size without having to check what it is first – tim_yates Jul 15 '15 at 20:56
  • Neither size() nor length() are very consistent, really... they should be getSize() and getLength()... but String pre-dates the Bean spec, and Thou Shalt Not Change Old Java. – billjamesdev Jul 15 '15 at 23:13

2 Answers2

16

Groovy adds lots of methods to strings and all sorts of other classes. All of the convenience methods are part of why Groovy is great.

java.lang.String implements java.lang.CharSequence, and that's where it gets all (most of) the magic from. size(), for example. Groovy adds a size() method to most objects that can have something you'd consider a size so that you can use a consistent method across the board. length() is still entirely valid, Groovy doesn't remove this.

To see some of the methods Groovy adds, check out the GDK, and particularly CharSequence and Collection.

doelleri
  • 19,232
  • 5
  • 61
  • 65
0

I suggest you read the doc of the Groovy class StringGroovyMethods, it gives simple explanations of how things work with Groovy.

Static methods are used with the first parameter being the destination class, e.g.. public static String reverse(String self) provides a reverse() method for String.

Thiht
  • 252
  • 4
  • 11