Chapter 2. The Groovy essentials


This chapter covers

  • Basics of the Groovy language
  • Differences between Java and Groovy
  • Groovy’s power features

As you saw in chapter 1, you can get a Grails application running in no time. You can even take the principles you learned in that chapter to add extra features. But if you want to develop something more complex, such as the Twitter-clone we use as the example project in this book, you’ll need to have a good grasp of the Groovy language.

Groovy is a dynamic, object-oriented language with a Java-like syntax. Furthermore, it integrates well with Java and the Java ecosystem: it runs on the JVM and uses JAR files for libraries. Java classes can depend on Groovy classes and vice versa. It’s also fun! This chapter is an introduction to the language based on a couple of worked examples. We do assume that you already know at least one object-oriented language such as Java, Ruby, or PHP.

The aim of this chapter is to prepare you for the examples throughout the rest of the book, but the information contained in the chapter is practical enough that you can start using Groovy day-to-day for other projects or as a portable language for writing scripts. We’re sure you’ll find plenty of uses for it! We also introduce the Spock testing framework, which we use throughout the book. If you know Groovy but not Spock, we recommend that you read section 2.2 before moving on to the next chapter.

2.1. Writing your first script

2.2. Creating a quote analyzer class

2.3. Going to the next level

2.4. Summary and best practices