Groovy Operators

Control Statements

Groovy File Handling

Groovy Error & Exceptions

Groovy Multithreading

Groovy Synchronization

Groovy - Relational Operators



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

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 - Usage of Equality and Non-Equality Operators

In this example, we're creating two variables a and b and using relational operators. We've performed equality and non-equality checks and printed the results.

Example.groovy

class Example { 
   static void main(String[] args) { 
      def a = 10;
      def b = 20;

      println("a == b = " + (a == b) );
      println("a != b = " + (a != b) );
   } 
} 

When we run the above program, we will get the following result. It can be seen that the results are as expected from the description of the operators as shown above.

a == b = false
a != b = true

Example - Usage of Greater Than and Less Than Operators

In this example, we're creating two variables a and b and using relational operators. We've performed equality and non-equality checks and printed the results.

Example.groovy

class Example { 
   static void main(String[] args) { 
      def a = 10;
      def b = 20;

      println("a > b = " + (a > b) );
      println("a < b = " + (a < b) );
   } 
} 

When we run the above program, we will get the following result. It can be seen that the results are as expected from the description of the operators as shown above.

a > b = false
a < b = true

Example - Usage of Greater Than Equal to and Less Than Equal to Operators

In this example, we're creating two variables a and b and using relational operators. We've performed equality and non-equality checks and printed the results.

Example.groovy

class Example { 
   static void main(String[] args) { 
      def a = 10;
      def b = 20;

      println("a >= b = " + (a >= b) );
      println("a <= b = " + (a <= b) );
   } 
} 

When we run the above program, we will get the following result. It can be seen that the results are as expected from the description of the operators as shown above.

a >= b = false
a <= b = true
groovy_operators.htm
Advertisements