Java Reference
In-Depth Information
In each case the println method prints to the console and returns null . When there's
no ambiguity, the parentheses can be omitted. Semicolons work as in Java, but they're op-
tional.
This is an example of a Groovy script . A script is a code listing that doesn't explicitly in-
clude a class definition. In Java, everything has to be inside a class. Groovy is able to work
with both scripts and classes.
A Groovy script is a form of syntactic sugar . [ 5 ] A class is, in fact, involved. If I compile
this script and then run the javap command on it, I get the following response:
5 Syntactic sugar is syntax that simplifies writing code but doesn't change anything under the hood. There may be
some evidence that an overuse of syntactic sugar leads to syntactic diabetes.
> groovyc hello_world.groovy
> javap hello_world
Compiled from "hello_world.groovy"
public class hello_world extends groovy.lang.Script{
public static transient boolean __$stMC;
public static long __timeStamp;
public static long __timeStamp__239_neverHappen1309544582162;
public hello_world();
public hello_world(groovy.lang.Binding);
public static void main(java.lang.String[]);
public java.lang.Object run();
...
There are about 30 more lines of output from the javap command, mostly involving
superclass methods. The interesting part is that the groovy command generates a class
called hello_world , along with a pair of constructors and a main method. The class
is generated at compile time and extends a class from the Groovy library called
groovy.lang.Script . In effect, scripts in Groovy become classes in Java, where the
code in the script ultimately (after a few layers of indirection) is executed by the main
method. I don't want to give the impression that Groovy is generating Java, however.
Groovy code is compiled directly into bytecodes for the JVM.
Compiled Groovy
Groovy is compiled, not interpreted. It's not a code generator; the compiler generates Java
bytecodes directly.
 
Search WWH ::




Custom Search