Java Reference
In-Depth Information
Closure Arguments
If the last argument to a method is a closure, it can be placed after the parentheses.
The
upto
and
downto
methods take two arguments, so the parentheses are shown in the
former and a comma is used in the latter to indicate that both the number and the closure
are arguments to the method. The
countDown
variable is a list, which will be discussed
in
section B.3
. The left-shift operator has been overloaded to append to the collection, and
its argument here is a parameterized string. Groovy has two types of strings, discussed in
the next section.
B.2.2. Strings and Groovy strings
In Java, single quotes delimit characters (a primitive) and double quotes surround instances
of
java.lang.String
. In Groovy, both single and double quotes are used for strings,
butthere'sadifference.Double-quotedstringsareusedforparameter replacement. They're
not
instances
of
java.lang.String
,
but
rather
instances
of
groovy.lang.GString
.
Here are a couple of examples to show how they're used:
def s = 'this is a string'
assert s.class == java.lang.String
def gs = "this might be a GString"
assert gs.class == java.lang.String
assert !(gs instanceof GString)
gs = "If I put in a placeholder, this really is a GString: ${1+1}"
assert gs instanceof GString
Single-quoted strings are always instances of
java.lang.String
. Double-quoted
strings may or may not be Groovy strings, depending on whether parameter replacement is
done or not.