Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Java wayGroovy wayNote
Code Block
languagejava
statement;
Code Block
languagegroovy
statement

 

 

 

 

; is not mandatory at the end of a statement.
Code Block
languagejava
return var;
Code Block
languagegroovy
var
If method is not void and no return statement,
Groovy return the last exec statement in a function 
Code Block
languagejava
Object var;
int var;
List var ;
Code Block
languagejava
int foo(int var) {
	return var;
}
Code Block
languagegroovy
def var
int number
 
Code Block
languagegroovy
def foo(def var) {
	var
}
Auto typing. Java way is OK in Groovy too
Code Block
languagejava
class User {
    private String name;

    public String getName() {...}
    public void setName(String name) {...} 
 
}
Code Block
languagegroovy
class User {
    private String name;

    //getter/setter auto generated
} 
 
Getter and setter auto generated for private members.

 

 

Code Block
languagegroovy
def object = ...
object.newField = "test"

 

 

You can add new field or new method to an object at Runtime.
Code Block
languagejava
java.io.*
java.lang.*
java.math.BigDecimal
java.math.BigInteger
java.net.*
java.util.*
groovy.lang.*
groovy.util.*

 

Groovy auto import all these packages. 

Aliases can be used in imports with "as" keyword (as in Python).

Code Block
languagejava
if (annotation.getTerm() != null 
	&& annotation.getTerm().getUser() != null
	&& annotation.getTerm().getUser().getId() == 1) 
{ ... }
Code Block
languagegroovy
if (annotation.getTerm()?.getUser()?.getUser()
	== 1)
{ ... }
Safe navigation operator " ?. "
Code Block
languagejava
String name = p.getName() != null ? p.getName() : "";
Code Block
languagegroovy
def name = p.getName() ?: ""

Elvis operator " ?: ".

To assign a value or return a default value if it

is evaluated to false.

Code Block
languagejava
try {
	methodMayThrowException()
} catch(...) {...}
Code Block
languagegroovy
methodMayThrowException()
 
Code Block
languagejava
String s = "hello " + name 
String s = "hello " + user.getName(); 
Code Block
languagegroovy
String s = "hello $name"
String s = "hello ${user.getName()} 
GStrings
Code Block
languagejava
List<String> list = new list<String>();
Code Block
languagegroovy
def list = []
def listNotEmpty = [1,2,3] 
println listNotEmpty[0] // prints 1
println listNotEmpty[-1] // prints 3
 
// Find in list
if (x in list) { ... }

Syntacttic sugar for lists.

Similar to Python lists.

Code Block
languagejava
Map<Long,String> map = new Map<Long, String>();


String x = map.get(2); 
Code Block
languagegroovy
def map = [:]
def mapNotEmpty = [1:"a",2:"b",3:"c"]

def x = map[2] //or map.get(2)
 
def k = 'key3'
def map = [
	'key1': 1,
	key2: 2, // Skip quotes, seen as String
	(k): 3 // Put () for variable
]
assert map[k] == map['key3'] == map.key3
Syntactic sugar for maps.
Code Block
languagejava
for(String item : list) {
     doSomething(item);
}
Code Block
languagegroovy
list.each {
     doSomething(it);
}

Apply function on each element of a list.

By default, the current item in each{} is it.
You can change this by:

Code Block
.each {item -> ...}

You can get the index (current position in list):

Code Block
.eachWithIndex {item, i -> ...}
Code Block
languagejava
List<User> users = ...
List<String> names = ...

for(User user : users) {
     names.add(user.getName());
}
Code Block
languagegroovy
def names = users.collect{it.getName())
Build a list from anohter one.
Code Block
languagejava
System.out.println(..."hello");
Code Block
languagegroovy
println("hello")
println "hello"
Print.

 

+/- Java 8 lambdas

Code Block
languagegroovy
def square = { it * it }
println square(2) // prints 2
 
def power = { x, y ->
	Math.pow(x, y)
}
println power(2, 3) // prints 8
Closures.
Code Block
languagejava
Boolean x = (a == b)
 
; // Equality of primitive types
x = a.equals(b); // Equality of instances
Code Block
languagegroovy
def x = a.is(b) // Equality of primitive types
x = (a == b) // Equality of instances
Equals behaviour.