Java Reference
In-Depth Information
Appendix B. Groovy by feature
Some people learn by example. Some people learn by feature. In this topic I'm trying to sat-
isfy both. If you're a Java developer with only passing familiarity with Groovy, hopefully
Groovy language.
This appendix walks through most of the major features of Groovy and provides short snip-
pets of code illustrating them. While this chapter does not claim to be an exhaustive refer-
ence like
Groovy in Action
(Manning, 2007; called
GinA
in the rest of this appendix), it has
a couple of features that favor it over the more comprehensive treatment: (1) it's consider-
ably shorter, and (2) it has the words “Don't Panic!” written in nice, friendly letters in the
features of the Groovy programming language that are used throughout the topic.
1
For those born too late, that was a
Hitchhiker's Guide to the Galaxy
reference. I could go on to say that this chapter
“contains much that is apocryphal, or at least wildly inaccurate,” but that probably wouldn't be good for sales.
Because this isn't going to be a comprehensive treatment, I've chosen aspects of Groovy
to review based on two criteria: (1) how often they're used in practice and (2) how much
they offer an advantage over corresponding features in Java (assuming the corresponding
feature in Java even exists). After getting the basics of Groovy out of the way (like how to
run Groovy programs and basic data types like numbers and strings), I'll move on to issues
like collections, I/O, XML, and more. Some topics, like SQL, are covered in other chapters,
but you'll find the essentials here.
B.1. Scripts and the traditional example
program, as shown here:
2
See
appendix A
for details.
println 'Hello, Groovy!'