
- Groovy Tutorial
- Groovy - Home
- Groovy - Overview
- Groovy - Environment
- Groovy - Basic Syntax
- Groovy - Data Types
- Groovy - Variables
- Groovy - Optionals
- Groovy - Numbers
- Groovy - Strings
- Groovy - Ranges
- Groovy - Lists
- Groovy - Maps
- Groovy - Dates & Times
Groovy Operators
- Groovy - Operators
- Groovy - Arithmetic Operators
- Groovy - Assignment Operators
- Groovy - Relational Operators
- Groovy - Logical Operators
- Groovy - Bitwise Operators
- Groovy - Spaceship Operator
- Groovy - in Operator
- Groovy - Elvis Operator
- Groovy - Safe Navigation Operator
- Groovy Operator Precedence & Associativity
Control Statements
- Groovy - Decision Making
- Groovy - If Else Statement
- Groovy - Switch Statement
- Groovy - Loops
- Groovy - For Loop
- Groovy - For-in Loop
- Groovy - While Loop
- Groovy - Do While Loop
- Groovy - Break Statement
- Groovy - Continue Statement
Groovy File Handling
- Groovy - File I/O
- Java - Create a File
- Java - Write to File
- Java - Append to File
- Java - Read Files
- Java - Delete Files
- Java - File Properties
- Java - File Existence and Type
- Java - File Size
- Java - File Permissions
- Java - Directories
- Java - Listing Directories
- Java - Filtering Files/Directories
- Java - Deleting Directories
- Java - Renaming Files/Directories
Groovy Error & Exceptions
- Groovy - Exception Handling
- Groovy - try-catch Block
- Groovy - try-with-resources
- Groovy - Multi-catch Block
- Groovy - Nested try Block
- Groovy - Finally Block
- Groovy - throw Exception
- Groovy - Exception Propagation
- Groovy - Built-in Exceptions
- Groovy - Custom Exception
Groovy Multithreading
- groovy - Multithreading
- groovy - Thread Life Cycle
- groovy - Creating a Thread
- groovy - Starting a Thread
- groovy - Joining Threads
- groovy - Naming Thread
- groovy - Thread Scheduler
- groovy - Thread Pools
- groovy - Main Thread
- groovy - Thread Priority
- groovy - Daemon Threads
- groovy - Shutdown Hook
Groovy Synchronization
- groovy - Synchronization
- groovy - Block Synchronization
- groovy - Static Synchronization
- groovy - Inter-thread Communication
- groovy - Thread Deadlock
- groovy - Interrupting a Thread
- groovy - Thread Control
- groovy - Reentrant Monitor
- Groovy - Methods
- Groovy - Methods
- Groovy - Optional parenthesis
- Groovy - Named Arguments
- Groovy - Closures as Arguments
- Groovy - Method Overloading
- Groovy - Method Scope and Visibility
- Groovy - isCase Method
- Groovy - Implicit Return
- Groovy - Variable Arguments
- Groovy - Regular Expressions
- Groovy - Regular Expressions
- Groovy - Defining Regular Expressions
- Groovy - Matcher Object
- Groovy - Regex Tasks
- Groovy - XML
- Groovy - XML
- Groovy - Parsing XML
- Groovy - Creating XML
- Groovy - Modifying XML
- Groovy - Querying XML
- Groovy - Simplified Notation
- Groovy - Closure based Querying
- Groovy - Closure based Creation
- Groovy - JSON
- Groovy - JSON
- Groovy - Parsing JSON
- Groovy - Creating JSON using JsonOutput
- Groovy - Creating JSON using JsonBuilder
- Groovy - Modifying JSON
- Groovy - Error Handling
- Groovy - Handling JSON Arrays
- Groovy - JSON Array Operations
- Groovy - JSON Objects
- Groovy - JSON Object Operations
- Groovy - Generics
- Groovy - Generics
- Groovy - Declaring Generic Types
- Groovy - Bound Type Parameters
- Groovy - Wild Cards
- Groovy - Miscellaneous
- Groovy - Object Oriented
- Groovy - Closures
- Groovy - Annotations
- Groovy - JMX
- Groovy - DSLS
- Groovy - Database
- Groovy - Builders
- Groovy - Command Line
- Groovy - Unit Testing
- Groovy - Template Engines
- Groovy - Meta Object Programming
- Groovy Useful Resources
- Groovy - Quick Guide
- Groovy - Useful Resources
- Groovy - Discussion
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 −
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 −
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 −
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 −
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 −
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