views:

1042

answers:

5

Most Java code is also syntactically valid Groovy code. However, there are a few exceptions which leads me to my question:

Which constructs/features in Java are syntactically invalid in Groovy? Please provide concrete examples of Java code (Java 1.6) that is NOT valid Groovy code (Groovy 1.6).

Update:

So far we've got five examples of syntactically valid Java code that is not valid Groovy code:

  • 1.) Array initializations
  • 2.) Inner classes
  • 3.) "def" is a keyword in Groovy, but not in Java
  • 4.) "$$"-strings - parsed as an invalid GStrings in Groovy
  • 5.) Non-static initialization blocks -- class Foo { Integer x; { x = 1; } }

Is this the complete list? Any further examples?

Update #1: I've started a bounty to bump this question. The bounty will be granted to the person who provides the most comprehensive list of examples. So far we've uncovered five examples, but I'm sure there a quite some more out there. So keep them coming!

+10  A: 

Ok, heres one point:

int[] i = { 0, 1, 2 };

That is good syntax in java, bad in groovy.

I don't think you want to assume any given java code will be equivalent in groovy. This site describes some of the differences, which includes things as basic as == not meaning the same thing in both languages. Also, static array initialization is different, and there are no anonymous inner classes.

This compiles fine in Java 1.6

public class Test2 {
    int[] i = { 0, 1, 2 };

    private class Class1 {
     public void method1() {
      if (i[2] == 2) {
       System.out.println("this works");
      }
     }
    }

    public void method1() {
     Class1 class1 = new Class1();
     class1.method1();
    }
}

But is so wrong in Groovy. It gives the following errors in Groovy 1.6:

unexpected token: 1 @ line 2, column 14.

Class definition not expected here. Possible attempt to use inner class. Inner classes not supported, perhaps try using a closure instead. at line: 4 column: 2.

If you fix those things, it does print what you expect, though.

If you're looking for newer language syntax issues, like generics or annotations, Groovy supports both of those, though not fully.

John Ellinwood
gota love the drive by down votes that give no reason...
TofuBeer
It wasn't me who downvoted, but: that's not what was asked. Nobody said Java code would be equivalent in groovy. The question was what snytactically valid Java code is not syntactically valid in Groovy.
Michael Borgwardt
I do not interpret the question that way: "Which constructs/features in Java are not supported in Groovy?" I read that as behave differently. Perhaps the question needs to be clearer.
TofuBeer
+1  A: 

All I can think of for now:

  • int def;
  • String s = "$$";
Michael Borgwardt
+1  A: 

Non-static initialization blocks:

class Foo {
  Integer x;   
  { x = 1; }
}

UPDATE: This is in fact valid Groovy code.

Hamlet D'Arcy
The heck? I didn't know that was valid Java, either.
CaptainAwesomePants
Testing confirms that it is indeed run whenever an instance of the class is created. Freaky. The more you know.
CaptainAwesomePants
This DOES work in Groovy. At least it works in both Groovy 1.5.7 and 1.6.
Peter Dolberg
hmm, it does work for me too. I could have sworn this used to not work. Oh well, down vote the answer.
Hamlet D'Arcy
+13  A: 

Here is a list of items that are valid Java 6, but not valid Groovy 1.6. This isn't a complete list, but I think it covers most of the cases.

(By the way, I think you should note that non-static initialization blocks DO work in Groovy.)

Any inner class declaration

including static,

public class Outer{
  static class Inner{}
}

non-static,

public class Outer{
  class Inner{}
}

local classes,

public class Outer{
  public static void main(String[] args) {
    class Local{}  
  }
}

and anonymous classes

java.util.EventListener listener=new java.util.EventListener(){};

Using Groovy keywords as variables

int def;
int in;
int threadsafe;
int as;

Array initialization

String[] stuff=new String[]{"string"};
int[] array={1,2,3};

Using dollar signs in strings where what follows isn't a valid expression

String s="$$";
String s="$def";
String s="$enum";
String s="$;";
String s="$\\";
//etc.

More than one initializer in a for loop

for (int i=0, j=0; i < 5; i++) {}

More than one increment in a for loop

int j=0;
for (int i=0; i < 5; i++,j++) {}

Breaking up some expressions using newlines

int a= 2 
/ 2 
;

Ending switch with a case that has no body

switch(a){
  case 1:
}

Having a default in a switch with no body

Applies in both cases where default is at the end

int a=0;
switch(a){
    default:
}

or somewhere in the middle

switch(a){
    default:
    case 1:
     break;
}

Annotations with lists

@SuppressWarnings({"boxing","cast"})

Native method declaration

public native int nativeMethod();

Class per enum

public enum JavaEnum{
  ADD{
    public String getSymbol(){ return "+"; }
  };
  abstract String getSymbol();
}

Do loop

do{
  System.out.println("stuff");
}while(true);
Peter Dolberg
+1  A: 

Multidimensional arrays, where size is not specified.

def x=new Object[5][];  // ERROR: expression expected 

def x=new Object[5][2]; // this works