Groovy Operators

Control Statements

Groovy File Handling

Groovy Error & Exceptions

Groovy Multithreading

Groovy Synchronization

Groovy - Operators



An operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations.

Groovy has the following types of operators −

  • Arithmetic operators
  • Relational operators
  • Logical operators
  • Bitwise operators
  • Assignment operators

Arithmetic Operators

The Groovy language supports the normal Arithmetic operators as any the language. Following are the Arithmetic operators available in Groovy −

Show Example

Operator Description Example
+ Addition of two operands 1 + 2 will give 3
Subtracts second operand from the first 2 1 will give 1
* Multiplication of both operands 2 * 2 will give 4
/ Division of numerator by denominator 3 / 2 will give 1.5
% Modulus Operator and remainder of after an integer/float division 3 % 2 will give 1
++ Incremental operators used to increment the value of an operand by 1

int x = 5;

x++;

x will give 6

-- Incremental operators used to decrement the value of an operand by 1

int x = 5;

x--;

x will give 4

Example

The following example demonstrates the usage of arithmetic operators in Groovy.

Example.groovy

class Example { 
   static void main(String[] args) { 
      // Initializing 3 variables 
      def x = 5; 
      def y = 10; 
      def z = 8; 
		
      //Performing addition of 2 operands 
      println(x+y); 
		
      //Subtracts second operand from the first 
      println(x-y); 
		
      //Multiplication of both operands 
      println(x*y);
		
      //Division of numerator by denominator 
      println(y/x); 
   } 
} 

Output

When we run the above program, we will get the following result −

15
-5
50
2

Relational operators

Relational operators allow of the comparison of objects. Following are the relational operators available in Groovy −

Show Example

Operator Description Example
== Tests the equality between two objects 2 == 2 will give true
!= Tests the difference between two objects 3 != 2 will give true
< Checks to see if the left objects is less than the right operand. 2 < 3 will give true
<= Checks to see if the left objects is less than or equal to the right operand. 2 <= 3 will give true
> Checks to see if the left objects is greater than the right operand. 3 > 2 will give true
>= Checks to see if the left objects is greater than or equal to the right operand. 3 >= 2 will give true

Example

The following example demonstrates the usage of relational operators in Groovy.

Example.groovy

class Example { 
   static void main(String[] args) { 
      def x = 5;
      def y = 10;
		
      println("x == y: " + (x == y)); // false
      println("x != y: " + (x != y)); // true
      println("x > y: " + (x > y));   // true
      println("x < y: " + (x < y));   // false
      println("x >= y: " + (x >= y)); // true
      println("x <= y: " + (x <= y)); // false
   } 
} 

Output

When we run the above program, we will get the following result −

x == y: false
x != y: true
x > y: false
x < y: true
x >= y: false
x <= y: true

Logical Operators

Logical operators are used to evaluate Boolean expressions. Following are the logical operators available in Groovy −

Show Example

Operator Description Example
&& This is the logical and operator true && true will give true
|| This is the logical or operator true || true will give true
! This is the logical not operator !false will give true

Example

The following example demonstrates the usage of logical operators in Groovy.

Example.groovy

class Example { 
   static void main(String[] args) { 
      def x = true;
      def y = false;
		
      println("x && y: " + (x && y)); // false
      println("x || y: " + (x || y)); // true
      println("!x: " + (!x));   // false
      println("!y: " + (!y));   // true
   } 
} 

Output

When we run the above program, we will get the following result −

x && y: false
x || y: true
!x: false
!y: true

Bitwise Operators

Groovy provides four bitwise operators. Following are the bitwise operators available in Groovy −

Show Example

Sr.No Operator & Description
1

&

This is the bitwise and operator

2

|

This is the bitwise or operator

3

^

This is the bitwise xor or Exclusive or operator

4

~

This is the bitwise negation operator

Here is the truth table showcasing these operators.

p q p & q p | q p ^ q
0 0 0 0 0
0 1 0 1 1
1 1 1 1 0
1 0 0 1 1

Example

The following example demonstrates the usage of bitwise operators in Groovy.

Example.groovy

class Example { 
   static void main(String[] args) { 
      def x = 60;
      def y = 13;
		
      println("x & y: " + (x & y)); // 12  (0000 1100)
      println("x | y: " + (x | y)); // 61  (0011 1101)
      println("x ^ y: " + (x ^ y)); // 49  (0011 0001)
      println("~x: " + (~x)); // -61  (1100 0011 in 2's complement)
      println("x << 2: " + (x << 2)); // 240 (1111 0000)
      println("x >> 2: " + (x >> 2)); // 15  (0000 1111)
      println("x >>> 2: " + (x >>> 2)); // 15  (0000 1111)
   } 
} 

Output

When we run the above program, we will get the following result −

x & y: 12
x | y: 61
x ^ y: 49
~x: -61
x << 2: 240
x >> 2: 15
x >>> 2: 15

Assignment operators

The Groovy language also provides assignment operators. Following are the assignment operators available in Groovy −

Show Example

Operator Description Example
+= This adds right operand to the left operand and assigns the result to left operand.

def A = 5

A+=3

Output will be 8

-= This subtracts right operand from the left operand and assigns the result to left operand

def A = 5

A-=3

Output will be 2

*= This multiplies right operand with the left operand and assigns the result to left operand

def A = 5

A*=3

Output will be 15

/= This divides left operand with the right operand and assigns the result to left operand

def A = 6

A/=3

Output will be 2

%= This takes modulus using two operands and assigns the result to left operand

def A = 5

A%=3

Output will be 2

Example

The following example demonstrates the usage of assignment operators in Groovy.

Example.groovy

class Example { 
   static void main(String[] args) { 
      int x = 10;
        
      // Assign and add
      x += 5;
      println("x += 5: " + x); // 15
        
      // Assign xnd subtrxct
      x -= 3;
      System.out.println("x -= 3: " + x); // 12
        
      // Assign xnd multiply
      x *= 2;
      System.out.println("x *= 2: " + x); // 24
        
      // Assign xnd divide
      x /= 4;
      System.out.println("x /= 4: " + x); // 6
        
      // Assign xnd modulus
      x %= 5;
      System.out.println("x %= 5: " + x); // 1
   } 
} 

Output

When we run the above program, we will get the following result −

x += 5: 15
x -= 3: 12
x *= 2: 24
x /= 4: 6
x %= 5: 1

Range Operators

Groovy supports the concept of ranges and provides a notation of range operators with the help of the .. notation. A simple example of the range operator is given below.

def range = 0..5 

This just defines a simple range of integers, stored into a local variable called range with a lower bound of 0 and an upper bound of 5.

Example

The following code snippet shows how the various operators can be used.

Example.groovy

class Example { 
   static void main(String[] args) { 
      def range = 5..10; 
      println(range); 
      println(range.get(2)); 
   } 
}

Output

When we run the above program, we will get the following result −

From the println statement, you can see that the entire range of numbers which are defined in the range statement are displayed.

The get statement is used to get an object from the range defined which takes in an index value as the parameter.

[5, 6, 7, 8, 9, 10] 
7

Operator Precedence

The following table lists all groovy operators in order of precedence.

Sr.No Operators & Names
1

++ -- + -

pre increment/decrement, unary plus, unary minus

2

* / %

multiply, div, modulo

3

+ -

addition, subtraction

4

== != <=>

equals, not equals, compare to

5

&

binary/bitwise and

6

^

binary/bitwise xor

7

|

binary/bitwise or

8

&&

logical and

9

||

logical or

10

= **= *= /= %= += -= <<= >>= >>>= &= ^= |=

Various assignment operators

Example

The following example demonstrates Operator Precedence in Groovy.

Example.groovy

class Example { 
   static void main(String[] args) { 
      int result1 = 10 + 5 * 2; // Multiplication (*) has higher precedence than addition (+)
      int result2 = (10 + 5) * 2; // Parentheses () change precedence
      int result3 = 20 / 4 * 2; // Left-to-right associativity
      int result4 = 10 - 3 + 2; // Left-to-right associativity

      println("10 + 5 * 2 = " + result1);
      println("(10 + 5) * 2 = " + result2);
      println("20 / 4 * 2 = " + result3);
      println("10 - 3 + 2 = " + result4);
   } 
} 

Output

When we run the above program, we will get the following result −

10 + 5 * 2 = 20
(10 + 5) * 2 = 30
20 / 4 * 2 = 10
10 - 3 + 2 = 9
Advertisements