Saturday, February 4, 2012

Filled Under:

Groovy and Java: A Comparison
Groovy’s resemblance to Java is often quite striking. Some Groovy code is almost indiscernible
from Java. If your Groovy code looks too much like Java, you can improve its expressiveness by
writing more idiomatic Groovy. Let’s take a look at the key similarities and differences between
Groovy and the Java language.
What’s the Same?
There are actually many similarities between Java and Groovy. This is what makes Groovy so
appealing from a Java developer’s perspective. There is no huge mental shift necessary to start
working with Groovy. The Groovy syntax can almost be seen as a superset of the Java language
(although this is not the case), with the following taken directly from Java’s syntax:

• Keywords and statements
• Try/catch/finally exception handling
• Class, interface, field, and method definitions
• Instantiation of objects using the new operator
• Packaging and imports
• Operators, expressions, and assignment
• Control structures
• Comments

More importantly though, Groovy shares the same object and runtime model as Java,
meaning the infrastructure within which you are operating (the JVM) is the same. What does
this mean? Well, although Groovy is a dynamic language similar in nature to Ruby or Python, it
is not interpreted. All Groovy code, be it executed as a script or a fully qualified class, is
compiled down to byte code and then executed.
The significance of this shouldn’t be underestimated, as it means that a Groovy class is a
Java class and they can interoperate with each other. A Java class can call methods on an interface
implemented by Groovy without ever knowing any different.
So that’s what is the same; again, I’ve given a brief overview, but really the similarities
become obvious quite quickly once you start working with Groovy. Of equal significance,
however, is what is different about Groovy.

What’s Different?

What makes Groovy different is that there are a number of things that are optional in Groovy,
including parentheses, return statements, and semicolons at the end of statements.




0 comments:

Post a Comment