Chapter 1. Introduction to Groovy
We can’t talk much about Grails without a solid understanding of Groovy, because it’s so integral to how Grails works.
Groovy is a JVM language with a primary goal of extending Java. By adding a Meta Object Protocol (MOP) to enable metaprogramming, Groovy adds powerful capabilities that enable dynamic programming (changing and adding behavior at runtime), domain-specific languages (DSLs), and a huge number of convenience methods and approaches that simplify your code and make it more powerful.
Groovy compiles to bytecode just like Java does (although it creates different bytecode). As Java developers, we tend to think that only javac can compile source code to create .class files, but there are many JVM languages that do as well (including Groovy, JRuby, Jython, and hundreds more). There are also libraries such as BCEL that you can use to programmatically create bytecode. As a result, Groovy and Java interoperate well; you can call Java methods from Groovy and vice versa, a Java class can extend a Groovy class or implement a Groovy interface, and in general, you don’t need to even think about interoperability because it “just works.”
The Groovy equivalent of javac is groovyc, and because it compiles both Groovy and Java code, it’s simple to use for your project code. Of course in Grails applications, we rarely even think about this process (except when it fails) because Grails scripts handle that, but if you’re manually compiling code (e.g., in a ...
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