32

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?

Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
  • 1
    possible duplicate of [Valid Java code that is NOT valid Groovy code?](http://stackoverflow.com/questions/687601/valid-java-code-that-is-not-valid-groovy-code) – Michael Borgwardt Nov 26 '10 at 14:17
  • 1
    @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
  • Possible duplicate of [Groovy Superset of Java](http://stackoverflow.com/questions/1959165/groovy-superset-of-java) – starcorn May 20 '16 at 12:16

4 Answers4

22

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
        +1;
        System.out.println(i);
    }
}

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.

ataylor
  • 64,891
  • 24
  • 161
  • 189
17

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.

Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
Goran Jovic
  • 9,418
  • 3
  • 43
  • 75
12

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

tim_yates
  • 167,322
  • 27
  • 342
  • 338
  • 3
    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
5

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)

Dónal
  • 185,044
  • 174
  • 569
  • 824
  • 1
    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
  • 8
    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