Operators

Let us understand the below terms before we get into more details.

1. Operator

An operator is a symbol which has special meaning and performs an operation on single or multiple operands like addition, substraction etc. In the below example, + is the operator.

def x, y, sum;
x = 10;
y = 20;
 
sum = x + y;
println "Sum is: " +sum;

Check result here

2. Operand

An operand is what operators are applied on. In the above example x and y are the operands.

Types of Operators in Groovy

1. Arithmetic Operators

Arithmetic operators are used to perform arithmetic operations on operands.

OperatorDescriptionExample
+Used to perform Addition8+2 = 10
-Used to perform Subtraction12-2 = 10
*Used to perform Multiplication5*2 = 10
/Used to perform Division100/10 = 10
%Used to return Remainder40%10 = 0
++Used to perform Incrementint a=10; a++; // a becomes 11
--Used to perform Decrementint a=10; a--; // a becomes 9

Example

def x, y, sum, diff, product, division, mod, inc, dec;
x = 90;
y = 10;
 
 sum = x + y;
println "Sum : " + sum ;
   
diff = x - y;
println "Difference : " + diff ;
   
product = x * y;
println "Product : " + product;
   
division = x / y;
println "Division : " + division;
   
mod = x % y;
println "Remainder : " + mod ;
   
inc = x++;
println "x value after incrementing : " + x;
   
dec = x--;
println "x value after decrementing : " + x;

Check Result here

2. Relational Operators

Relational operators are used to compare two operands.

OperatorDescriptionUsage
==Is equal tox == y
!=Not equal to!=x
>Greater thanx > y
>=Greater than or equal tox >= y
<Less thanx < y
<=Less than or equal tox <= y

Example

   int x = 90;
   int y = 10;
   
  if ( x == y) 
    println "x and y are equal";
  
  if ( x != y) 
    println "x and y are not equal";
  
  if ( x > y) 
    println "x is greater than y";
  
  if ( x < y) 
    println "x is less than y";

Check Result here

3. Bitwise Operators

Bitwise operators are used to perform bitwise operations on operands.

OperatorDescriptionUsage
&Bitwise AND(x > y) & (y > z)
|Bitwise OR(x > y)(y > z)
^Bitwise XOR(x > y) ^ (y > z)
~Bitwise NOT(~x)

4. Logical operators

Below are the logical operators present in Groovy.

OperatorDescriptionUsage
&&Logical AND(x > y) && (y > z)
||Logical OR(x > y) || (y > z)
!Logical NOT(!x)

5. Assignment Operators

Below are the assignment operators present in Groovy.

OperatorDescriptionUsage
=Assignint x = 10;
+=Add and assignint x=10; x+=30; // x becomes 40
-=Subtract and assignint x=40; x-=10; // x becomes 30
*=Multiply and assignint x=10; x*=40; // x becomes 400
/=Divide and assignint x=100; x /= 10;// x becomes 10
%=Modulus and assignint x=100; x%=10; // x becomes 0

Example

int x = 10; // assigning 10 to x 
println "x value: " + x ;
        
x+=30;
println "x value after += operation: " + x ;
        
x-=10;
println "x value after -= operation: " + x ;
        
x*=10;
println"x value after *= operation: " + x ;
        
x/=10;
println"x value after /= operation: " + x ;
        
x%=10;
println"x value after %= operation: " + x;   

Check Result here

Summary

Operator typeDescription
Arithmetic Operators+ , - , * , / , %
comparision Operators< , > , <= , >=, != , ==
Bitwise Operators& , ^ , |
Logical Operators&& , ||, !
Assignment Operators= , += , -= , *= , /= , %=