422 APPENDIX B: Introduction to Groovy
This example creates a list of names and iterates through the list to print out the names. A printName()
method is created that prints the name parameter. Lastly and the main point of this example, the list
is iterated, executing the printName() method as a closure. Using the method closure operator,
you are able to expose Java methods as closures.
Diamond Operator
The diamond operator (<>) is introduced in Groovy to avoid the repetition of parameterized types.
The parameterized types can be omitted and replaced with pointy brackets, which look like a
diamond. Listing B-41 shows a usual verbose way of defining a list.
Listing B-41. A Simple Groovy Script: Hello.groovy
List<List
Listing B-42 illustrates how to use the diamond operator.
Listing B-42. Using the Diamond Operator
List<List
Summary
This appendix introduced the basics of Groovy. One appendix is not enough to learn any language
or technology, but that said, the introduction to Groovy in this appendix is substantial enough to
write web applications using Grails. In this appendix, you first learned how to install Groovy; then
it showed how to write a “Hello World” program with Groovy. Then you learned how to run Groovy
scripts and classes and looked at various strings supported in Groovy. Then the chapter briefly
introduced the collective datatypes of Groovy, and you learned what a closure is and how to use it in
Groovy. Lastly, you learned how to use specialized operators available in Groovy.