As the Groovy language is largely compatible with the Java language, it's an easy language to learn for Java developers. Many elements that are required in Java are optional in Groovy. Since Groovy follows the same semantics as Java, we will concentrate mostly on the differences between Java and Groovy in this chapter. Groovy is all about being compact, comfortable, and flexible. Let's start by looking at a simple class in Java:
class Person { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } public static void main(String[] args) { Person p = new Person(); p.setName("fooBar"); System.out.println(p.getName()); } }
The preceding class will compile and run fine in Groovy; just enter the code in GroovyConsole and execute it. When using Groovy-specific constructs, the same program can be written with much less code, though...