This chapter covers the operators of the Groovy programming language.
Arithmetic operators
Groovy supports the usual familiar arithmetic operators you find in mathematics and in other programming languages like Java. All the Java arithmetic operators are supported. Let’s go through them in the following examples.
Normal arithmetic operators
The following binary arithmetic operators are available in Groovy:
Operator | Purpose | Remarks |
---|---|---|
|
addition |
|
|
substraction |
|
|
multiplication |
|
|
division |
Use |
|
modulo |
|
|
power |
See the section about the power operation for more information on the return type of the operation. |
Here are a few examples of usage of those operators:
assert 1 + 2 == 3
assert 4 - 3 == 1
assert 3 * 5 == 15
assert 3 / 2 == 1.5
assert 10 % 3 == 1
assert 2 ** 3 == 8
Unary operators
The +
and -
operators are also available as unary operators:
assert +3 == 3
assert -4 == 0 - 4
assert -(-1) == 1 (1)
1 | Note the usage of parentheses to surround an expression to apply the unary minus to that surrounded expression. |
In terms of unary arithmetics operators, the +
+ (increment) and --
(decrement) operators are available,
both in prefix and postfix notation:
def a = 2
def b = a++ * 3 (1)
assert a == 3 && b == 6
def c = 3
def d = c-- * 2 (2)
assert c == 2 && d == 6
def e = 1
def f = ++e + 3 (3)
assert e == 2 && f == 5
def g = 4
def h = --g + 1 (4)
assert g == 3 && h == 4
1 | The postfix increment will increment a after the expression has been evaluated and assigned into b |
2 | The postfix decrement will decrement c after the expression has been evaluated and assigned into d |
3 | The prefix increment will increment e before the expression is evaluated and assigned into f |
4 | The prefix decrement will decrement g before the expression is evaluated and assigned into h |
Assignment arithmetic operators
From the binary arithmetic operators we have seen above, certain of them are also available in an assignment form:
-
+=
-
-=
-
*=
-
/=
-
%=
Let’s see them in action:
def a = 4
a += 3
assert a == 7
def b = 5
b -= 3
assert b == 2
def c = 5
c *= 3
assert c == 15
def d = 10
d /= 2
assert d == 5
def e = 10
e %= 3
assert e == 1
Relational operators
Relational operators allow comparisons between objects, to know if two objects are the same or different, or if one is greater or lower than or equal to the other.
The following operators are available:
Operator | Purpose |
---|---|
|
equal |
|
different |
|
less than |
|
less than or equal |
|
greater than |
|
greater than or equal |
These operators into action in simple number comparisons:
assert 1 + 2 == 3
assert 3 != 4
assert -2 < 3
assert 2 <= 2
assert 3 <= 4
assert 5 > 1
assert 5 >= -2
Logical operators
Groovy offers three logical operators for boolean expressions:
-
&&
: logical "and" -
||
: logical "or" -
!
: logical "not"
Let’s illustrate them with the following examples:
assert !false (1)
assert true && true (2)
assert true || false (3)
1 | "not" false is true |
2 | true "and" true is true |
3 | true "or" false is true |
Precedence
The logical "not" has a higher priority than the logical "and".
assert !false && true (1)
1 | Here, the assertion is true, because "not" has a higher precedence than "and", otherwise, the assertion would have failed |
The logical "and" has a higher priority than the logical "or".
assert false || true && true (1)
1 | Here, the assertion is true, because "and" has a higher precedence than "or", otherwise, the assertion would have failed |
Short-circuiting for ||
The logical "or" operator is supporting short-circuiting: if the left operand is true, it won’t evaluate the right operand. The right operand will be evaluated only if the left operand is false.
called = false
boolean somethingTrueOrFalse(boolean b) { (1)
called = true
return b
}
assert true || somethingTrueOrFalse(false)
assert !called (2)
assert false || somethingTrueOrFalse(true)
assert called (3)
1 | We create a function that returns its boolean argument, but it sets the called flag |
2 | In the first case, we confirm that the function is not called, as || short-circuits the evaluation of the right operand. |
3 | In the second case, the right operand is called, as indicated by the fact our flag is now true |