Appendix A. Just Enough Groovy to Get By
This appendix reviews the basics of the Groovy programming language. The Gradle build files consist largely of a Domain Specific Language, written in Groovy, for builds. In addition to the DSL, any legal Groovy code can be added to the build.
Groovy is a general-purpose programming language, based on Java, that compiles to Java byte codes. While it has functional capabilities, it is an object-oriented language that is arguably the next-generation language in the path from C++ to Java.
Basic Syntax
The “Hello, World!” program for Groovy is the one-liner shown in Example A-1.
Example A-1. Hello, World! in Groovy
println'Hello, World!'
Items of note:
-
Semicolons are optional. If you add them, they work, but they’re not required.
-
Parentheses are optional until they’re not. If the compiler guesses correctly where they should have gone, everything works. Otherwise, add them back in. The
printlnmethod takes aStringargument. Here the parentheses are left out. -
There are two types of strings in Groovy: single-quoted strings, like Hello, are instances of
java.lang.String. Double-quoted strings are Groovy strings and allow interpolation, shown in Example A-2.
There are no “primitives” in Groovy. All variables use the wrapper classes, like java.lang.Integer, java.lang.Character, and java.lang.Double. The native data type for integer literals, like 3, is Integer. The native data type for floating point literals, like 3.5, is java.math.BigDecimal ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access