Is Groovy syntax an exact superset of Java syntax?

前端 未结 4 1835
春和景丽
春和景丽 2021-01-01 10:06

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

4条回答
  •  耶瑟儿~
    2021-01-01 10:30

    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.

提交回复
热议问题