Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Minor typo/grammar fix; "were used to write a for loop"

...

  • == means equals on all types. In Java there's a wierd part of the syntax where == means equality for primitive types and == means identity for objects. Since we're using autoboxing this would be very confusing for Java developers (since x == 5 would be mostly false if x was 5 (smile) . So for simplicity == means equals() in Groovy. If you really need the identity, you can use the method "is" like foo.is(bar). This does not work on null, but you can still use == here: foo==null.
  • in is a keyword. So don't use it as a variable name.
  • When declaring array you can't write

    Code Block
    
    int[] a = {1,2,3};
    

    you need to write

    Code Block
    
    int[] a = [1,2,3]
    
  • If you

    were

    are used to

    write

    writing a for loop

    which looked

    that looks like

    Code Block
    
    for (int i=0; i < len; i++) {...}
    

    in groovy you can use that too, but you can use only one count variable. Alternatives to this are

    Code Block
    
    for (i in 0..len-1) {...}
    

    or

    Code Block
    
    for (i in 0..<len) {...}
    

    or

    Code Block
    
    len.times {...}
    

Things to be aware of

...

Java programmers are used to semicolons terminating statements and not having closures. Also there are instance initializers in class definitions. So you might see something like:

Code Block
Java
Java

class Trial {
  private final Thing thing = new Thing ( ) ;
  { thing.doSomething ( ) ; }
}

Many Groovy programmers eschew the use of semicolons as distracting and redundant (though others use them all the time - it's a matter of coding style). A situation that leads to difficulties is writing the above in Groovy as:

Code Block
Java
Java

class Trial {
  private final thing = new Thing ( )
  { thing.doSomething ( ) }
}

...

The issue here is that in this situation the newline is not a statement terminator so the following block is treated as a closure, passed as an argument to the Thing constructor. Bizarre to many, but true. If you want to use instance initializers in this sort of way, it is effectively mandatory to have a semicolon:

Code Block
Java
Java

class Trial {
  private final thing = new Thing ( ) ;
  { thing.doSomething ( ) }
}

...

New features added to Groovy not available in Java

  • closuresClosures
  • native syntax for lists and maps
  • GroovyMarkup and GPath support
  • native support for regular expressions
  • polymorphic iteration and powerful switch statement
  • dynamic and static typing is supported - so you can omit the type declarations on methods, fields and variables
  • you can embed expressions inside strings
  • lots of new helper methods added to the JDK
  • simpler syntax for writing beans for both properties and adding event listeners
  • safe navigation using the ?. operator, e.g. "variable?.field" and "variable?.method()" - no more nested ifs to check for null clogging up your code