Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is Groovy syntax an exact superset of Java syntax?

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?

like image 280
Sean Patrick Floyd Avatar asked Nov 24 '10 22:11

Sean Patrick Floyd


People also ask

Is Groovy superset of Java?

Groovy is a general-purpose scripting language that runs on the Java Virtual Machine (JVM) and can largely be viewed as a superset of Java.

Is all Java valid Groovy?

Most valid Java files are also valid Groovy files. Although the two languages are similar, Groovy code can be more compact, because it does not need all the elements that Java needs.

Can we write Java code in Groovy file?

Groovy scripts can use any Java classes.

What is Groovy expression?

Groovy Programming Fundamentals for Java Developers A regular expression is a pattern that is used to find substrings in text. Groovy supports regular expressions natively using the ~”regex” expression. The text enclosed within the quotations represent the expression for comparison.


2 Answers

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.

like image 147
ataylor Avatar answered Sep 28 '22 04:09

ataylor


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.

like image 42
Goran Jovic Avatar answered Sep 28 '22 04:09

Goran Jovic