May 14, 2021 Groovy
An operator is a symbol that tells the compiler to perform specific mathematical or logical operations.
Groovy has the following types of operators -
The Groovy language supports normal arithmetic operators in any language. H ere are the arithmetic operators available in Groovy -
Operator | Describe | Example |
+ | The addition of two operasts | 1 plus 2 will get 3 |
- |
The first and second operations are subtracted
|
2 - 1 will get 1 |
* | Multiplication of two operations | 2 x 2 will get 4 |
/ | The divide between two operations | 3/2 will get 1.5 |
% | The molding operation | 3%2 will get 1 |
++ | Self-add operation, on the basis of its own value plus 1 |
INT X = 5; X ++; X will get 6 |
-- |
Self-subtract operation, minus 1 on the basis of its own value
|
INT X = 5; X - -; X will get 4 |
Operator | Describe | Example |
---|---|---|
== | Test whether the two objects are equal | 2 s 2 will get true |
!= | Test whether there is a difference between the two objects | 3! . . |
< | Check to see if the object on the left is smaller than the object on the right. | 2 slt; 3 will get true |
<= | Check to see if the left object is less than or equal to the right object | 2 slt;=3 will get true |
> | Check to see if the object on the left is bigger than the object on the right. | 3 sgt; 2 will get true |
>= | Check whether the left object is greater than or equal to the right object. | 3 sgt; s 2 will get true |
Logical operators are used to evaluate Boolean expressions. H ere are the logical operators available in Groovy -
Operator | Describe | Example |
---|---|---|
&& | This is a logical "and" operation | true and true get true |
|| | This is a logical "or" operation | true || true to get true |
! | This is the logical "non" operation | !true gets false |
Four bit operators are available in Groovy. H ere are the bit operators available in Groovy -
Operator | Describe |
---|---|
& | This is a bit "and" operation |
| | This is a bit-by-bit "or" operation |
^ | This is a bit-by-bit "different" or different or operator |
〜 | This is a bit-by-bit inverse operator |
Here is a table of true values that show 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 |
The Groovy language also provides assignment operators. H ere are the assignment operators available at Groovy -
Operator | Describe | Example |
---|---|---|
+= | The equivalent of A-B is A-B |
DEF A = 5 A += 3 The output will be 8 |
-= | A --B is equivalent to A-B |
DEF A = 5 A -= 3 The output will be 2 |
*= | A s b is equivalent to A s A s B |
DEF A = 5 A *= 3 The output will be 15 |
/= | A /-B is equivalent to A-A/B |
DEF A = 6 A /= 3 The output will be 2 |
(%)= | A (%) s B is equivalent to A s A % B |
DEF A = 5 A %= 3 The output will be 2 |
Groovy supports the concept of scope, and at the . . . S ymbols provide symbols for range operators with the help of symbols. A simple example of a range operator is given below.
def range = 0..5
This simply defines a simple integer range, stored in a local variable called the lower limit of the range to 0 and the upper limit to 5.
The following snipper shows how to use various operators.
class Example { static void main(String[] args) { def range = 5..10; println(range); println(range.get(2)); } }
When we run the program above, we get the following results -
From the println statement, you can see the entire range of numbers that are defined in the range statement.
The get statement is used to get an object from a defined range, using the index value as an argument.
[5, 6, 7, 8, 9, 10] 7
The following table lists all groovy operators in order of priority
Operator | Name |
---|---|
++ - + - | Pre-increase/decrease, one dollar plus, one dollar minus |
* / % | Multiplication, divide, mold |
+ - | Addition, subtract |
==! = <=> | Equal to, not equal to, comparison |
& | Binary/bit operators and |
^ | Binary/bit different or |
| | Binary/by bit or |
&& | Logic and |
|| | Logic or |
= ** = * = / =%= + = - = << = >> = >>> = = ^ = | = | Various assignment operators |