Note |
---|
You can Skip this tutorial if you already know Groovy. You can refer to this page as a high-level reference to Groovy concepts and go ahead with the tutorials to start writing Groovy scripts in JMWE. |
...
Table of Content Zone |
---|
| Operators
Groovy supports all the typical operators, such as arithmetic operators, assignment operators, logical operators, relational operators and conditional operator. See below examples using some of the operators.
Code Block |
---|
language | groovy |
---|
title | Arithmetic operators |
---|
linenumbers | true |
---|
|
1 + 2 == 3
3 / 2 == 1.5
10 % 3 == 1
|
Code Block |
---|
language | groovy |
---|
title | Assignment arithmetic operators |
---|
linenumbers | true |
---|
|
def a = 5
b = a += 3
b == 8
def c = 4
def d = c++
d == 5
def c = 5
c *= 3
c == 15
|
Code Block |
---|
language | groovy |
---|
title | Relational operators |
---|
linenumbers | true |
---|
|
a > b
b - 3 == a
b + 3 != a
|
Code Block |
---|
language | groovy |
---|
title | Logical operators |
---|
linenumbers | true |
---|
|
x and y
x || y
!x |
Code Block |
---|
language | groovy |
---|
title | Conditional operators |
---|
linenumbers | true |
---|
|
!true == false
!'' == true |
Panel |
---|
title | Ternary operator and Elvis operator |
---|
|
The ternary operator is a shortcut expression that is equivalent to an if/else branch assigning some value to a variable.
Instead of:
Code Block |
---|
language | groovy |
---|
linenumbers | true |
---|
|
def text = "Test"
if (text!=null && text.length()>0) {
result = text
} else {
result = 'Empty'
} |
you can simplify it to:
Code Block |
---|
language | groovy |
---|
title | Ternary operator |
---|
linenumbers | true |
---|
|
def text = "Test"
text ? text : 'Empty' |
Elvis operator is shortening of the ternary operator. You need not have to repeat the value you want to assign
Code Block |
---|
language | groovy |
---|
title | Elvis operator |
---|
linenumbers | true |
---|
|
def text = "Test"
text ?: 'Empty' |
There a few other notable operators that are specific only to the Groovy language; one of them is the Safe Navigation Operator.
Safe navigation operator
The Safe Navigation operator is used to avoid a NullPointerException. When you have a reference to an object you might need to verify that it is not null
before accessing methods or properties of the object. Using this operator, you can avoid this and directly return a null.
Normally you would have to:
Code Block |
---|
language | groovy |
---|
linenumbers | true |
---|
|
if(employee){
employee.getSalary()
} |
Instead, you can write it as:
Code Block |
---|
language | groovy |
---|
linenumbers | true |
---|
|
employee?.getSalary() | Groovy Control structures Groovy supports the usual if-else, "nested" if then else if, while loop, exception handling syntax. Code Block |
---|
language | groovy |
---|
linenumbers | true |
---|
| //Simple if
if(..){
(...)
}
//Simple if else
if(..){
(...)
}
else{
(...)
}
//Nested if
if (...) {
...
} else if (...) {
...
} else {
...
} |
Switch statement - The switch statement executes one statement from multiple conditions. It is like if-else-if ladder statement. The switch statement in Groovy can handle any kind of switch value and different kinds of matching can be performed. Code Block |
---|
language | groovy |
---|
linenumbers | true |
---|
| def x = 1.23
def result = ""
switch ( x ) {
case "foo":
result = "found foo"
// lets fall through
case "bar":
result += "bar"
case [4, 5, 6, 'inList']:
result = "list"
break
default:
result = "default"
} |
In Groovy it is backward compatible with Java code, one difference though is that the Groovy switch statement can handle any kind of switch value and different kinds of matching can be performed. for loop - for loop is used to iterate a part of the program several times. If the number of iteration is fixed, it is recommended to use for loop. In Groovy the for loop is much simpler and works with any kind of array, collection, Map, etc. Code Block |
---|
language | groovy |
---|
linenumbers | true |
---|
| // iterate over a range
def x = 0
for ( i in 0..9 ) {
x += i
}
x == 45
// iterate over an array
def array = (0..4).toArray()
x = 0
for ( i in array ) {
x += i
}
x == 10 |
Optional omitting of syntax Groovy allows you to leave out some elements of syntax like package prefix, parentheses, and semicolon which brings the code to the bare minimum.
|
...