Corporate Training
Request Demo
Click me
Menu
Let's Talk
Request Demo

Tutorials

Groovy - Operators and Expressions

5. Operators and Expressions

Operators in Groovy are symbols or keywords used to perform operations on data. Expressions are combinations of values, variables, operators, and function calls that produce a result. Groovy supports a wide range of operators and expressions:

1. Arithmetic Operators:

Arithmetic operators perform basic mathematical operations. Groovy supports the following arithmetic operators:

  • Addition ('+'): Adds two values.
  • Subtraction ('-'): Subtracts the right operand from the left operand.
  • Multiplication ('*'): Multiplies two values.
  • Division ('/'): Divides the left operand by the right operand.
  • Modulus ('%'): Calculates the remainder after division.
Example:
def a = 10
def b = 3

def addition = a + b
def subtraction = a - b
def multiplication = a * b
def division = a / b
def modulus = a % b

println("Addition: ${addition}")
println("Subtraction: ${subtraction}")
println("Multiplication: ${multiplication}")
println("Division: ${division}")
println("Modulus: ${modulus}")
 

2. Comparison Operators:

Comparison operators compare two values and return a Boolean result ('true' or 'false'). Groovy supports the following comparison operators:

  • Equal to ('=='): Checks if two values are equal.
  • Not equal to ('!='): Checks if two values are not equal.
  • Less than ('<'): Checks if the left operand is less than the right operand.
  • Greater than ('>'): Checks if the left operand is greater than the right operand.
  • Less than or equal to ('<='): Checks if the left operand is less than or equal to the right operand.
  • Greater than or equal to ('>='): Checks if the left operand is greater than or equal to the right operand.
Example:
def x = 5
def y = 8

def isEqual = x == y
def isNotEqual = x != y
def isLessThan = x < y
def isGreaterThan = x > y
def isLessThanOrEqual = x <= y
def isGreaterThanOrEqual = x >= y

println("Is Equal: ${isEqual}")
println("Is Not Equal: ${isNotEqual}")
println("Is Less Than: ${isLessThan}")
println("Is Greater Than: ${isGreaterThan}")
println("Is Less Than or Equal: ${isLessThanOrEqual}")
println("Is Greater Than or Equal: ${isGreaterThanOrEqual}")
 

3. Logical Operators:

Logical operators perform logical operations and return a Boolean result. Groovy supports the following logical operators:

  • Logical AND ('&&'): Returns 'true' if both operands are 'true'.
  • Logical OR ('||'): Returns 'true' if at least one operand is 'true'.
  • Logical NOT ('!'): Returns the opposite of the operand's value.
Example:
def isTrue = true
def isFalse = false

def andResult = isTrue && isFalse
def orResult = isTrue || isFalse
def notResult = !isTrue

println("AND Result: ${andResult}")
println("OR Result: ${orResult}")
println("NOT Result: ${notResult}")
 

4. Assignment Operators:

Assignment operators are used to assign values to variables. Groovy supports the following assignment operators:

  • Assignment ('='): Assigns the value on the right to the variable on the left.
  • Addition Assignment ('+='): Adds the value on the right to the variable on the left and assigns the result to the variable on the left.
  • Subtraction Assignment ('-='): Subtracts the value on the right from the variable on the left and assigns the result to the variable on the left.
  • Multiplication Assignment ('*='): Multiplies the variable on the left by the value on the right and assigns the result to the variable on the left.
  • Division Assignment ('/='): Divides the variable on the left by the value on the right and assigns the result to the variable on the left.
  • Modulus Assignment ('%='): Calculates the remainder of dividing the variable on the left by the value on the right and assigns the result to the variable on the left.
Example:
def num = 10

num += 5 // num is now 15
num -= 3 // num is now 12
num *= 2 // num is now 24
num /= 4 // num is now 6
num %= 3 // num is now 0
 

5. Ternary Operator:

The ternary operator ('? :') is a shorthand way to write conditional expressions. It returns one of two values based on a condition.

Example:
def age = 20
def isAdult = (age >= 18) ? true : false
println("Is Adult: ${isAdult}")
 

These are the most commonly used operators in Groovy. Understanding how to use them in expressions is crucial for performing calculations and making decisions in your code. Operators and expressions enable you to work with data and control program flow effectively.