Take the 2-minute tour ×

Being a Java programmer, I don't really have a Groovy background, but I use Groovy a lot lately to extend Maven (using GMaven). So far, I could use all the Java code I need in Groovy with the added Groovy sugar (metaclass methods, more operators, closures). My knowledge of Groovy is far from complete, but I like it, especially for Scripting purposes (I'm a bit careful about using a non-static typed language in an enterprise scenario, but that's not the topic here).

Anyway, the question is:

Is every bit of valid Java code automatically valid Groovy code? (I am talking about Source code, not compiled classes, I know Groovy can interact with Java classes.) Or are there Java constructs that are illegal in Groovy? Perhaps a reserved Groovy keyword that could be used as an identifier in Java, or something else? Or has Groovy deliberately been designed to be 100%-source compatible with Java?

share|improve this question
possible duplicate of Valid Java code that is NOT valid Groovy code? –  Michael Borgwardt Nov 26 '10 at 14:17
@Michael I agree it's a dupe, but the other question is over a year old (at least groovy has seen major updates since then). Is there a general policy on how long questions remain valid? –  Sean Patrick Floyd Nov 26 '10 at 17:30
add comment

4 Answers

up vote 12 down vote accepted

It isn't.

My favorite incompatibility: literal arrays:

String[] s = new String[] {"a", "b", "c"};

In Groovy, curly braces in this context would be expected to contain a closure, not a literal array.

share|improve this answer
And the groovy version would be String[] s = ["a", "b", "c"]; ? –  Sean Patrick Floyd Nov 25 '10 at 7:21
Is there a way to do it in Groovy? I mean: to have an actual array, not list. –  Rekin Nov 25 '10 at 7:22
^^ Almost. The [] syntax creates a list. However it is if you add "as String[]" at the end of line –  Goran Jovic Nov 25 '10 at 7:30
String[] s = ["a", "b", "c"] as String[] –  Goran Jovic Nov 25 '10 at 7:30
@Goran as you declare the type, you don't need the as in that example... String[] arr = [ 'a', 'b', 'c' ] is enough –  tim_yates Nov 25 '10 at 10:15
show 3 more comments

Nope. The following are keywords in groovy, but not Java:

any    as     def    in     with

Additionally, while not keywords, delegate and owner have special meaning in closures and can trip you up if you're not careful.

Additionally, there are some minor differences in the language syntax. For one thing, Java is more flexible about where array braces occur in declarations:

public static void main(String args[]) // valid java, error in groovy

Groovy is parsed differently, too. Here's an example:

public class Test {
    public static void main(String[] args) {
        int i = 0;
        i = 5

Java will print 6, groovy will print 5.

While groovy is mostly source compatible with java, there are lots of corner cases that aren't the same. That said, it is very compatible with the code people actually write.

share|improve this answer
Ah, line end ends statement (source of many awful bugs e.g. in JavaScript). Thanks for the hint (+1) –  Sean Patrick Floyd Nov 24 '10 at 23:46
The String args[] notation in Java is awful (reminds me of all the things I hate about C). Glad to hear Groovy doesn't have that. –  Sean Patrick Floyd Nov 25 '10 at 0:22
add comment

There's a page on the Groovy site which documents some of the differences, and another page which lists gotchas (such as the newline thing)

There are other things as well, one example being that Groovy doesn't support the do...while looping construct

share|improve this answer
Thanks. one example being that Groovy doesn't support the do...while looping construct good thing too. I'm starting to get the impression that Groovy took all the good stuff from Java and left out all the awful stuff. Which brings up the obvious question: Does Groovy have loop labels? –  Sean Patrick Floyd Nov 25 '10 at 13:49
Yeah, loop labels are in there, so continue label or break label are valid Groovy –  tim_yates Nov 25 '10 at 13:56
add comment

Others have already given examples of Java syntax that is illegal in Groovy (e.g. literal arrays). It is also worth remembering that some syntax which is legal in both, does not mean the same thing in both languages. For example in Java:

foo == bar

tests for identity, i.e. do foo and bar both refer to the same object? In Groovy, this tests for object equality, i.e. it returns the result of foo.equals(bar)

share|improve this answer
I know, and it's an awful feature of groovy IMHO. Fortunately I hardly ever rely on == in Java (for non-primitives) and so I won't make this mistake in Groovy either. –  Sean Patrick Floyd Nov 26 '10 at 14:13
I disagree. I think its a good feature. Testing for equality is the more common case and doesn't rely on the not-so-intuitive concept of identity. Plus object writers have complete control over it by overriding the equals() method. Testing for identity is rigidly defined by the language and exposes ugly java inconsistencies like new Long(1) != new Long(1) but Long.valueOf(1) == Long.valueOf(1). Groovy makes the "good" case use an operator (readable, intuitive) and the "bad" case use a method call (flagging it as an exception to take note of). Java does the reverse. –  ataylor Nov 26 '10 at 17:04
add comment

Your Answer


