In Groovy, operators are special symbols or keywords that are used to perform operations on variables and values. Groovy supports a wide range of operators, including arithmetic, relational, logical, bitwise, and more. Understanding these operators is crucial for writing effective and efficient Groovy code.

Types of Operators

  1. Arithmetic Operators

Arithmetic operators are used to perform basic mathematical operations.

Operator Description Example
+ Addition a + b
- Subtraction a - b
* Multiplication a * b
/ Division a / b
% Modulus (Remainder) a % b
++ Increment a++ or ++a
-- Decrement a-- or --a

Example:

def a = 10
def b = 3

println "Addition: ${a + b}"       // Output: 13
println "Subtraction: ${a - b}"    // Output: 7
println "Multiplication: ${a * b}" // Output: 30
println "Division: ${a / b}"       // Output: 3.3333333333333335
println "Modulus: ${a % b}"        // Output: 1

a++
println "Increment: ${a}"          // Output: 11

b--
println "Decrement: ${b}"          // Output: 2

  1. Relational Operators

Relational operators are used to compare two values.

Operator Description Example
== Equal to a == b
!= Not equal to a != b
> Greater than a > b
< Less than a < b
>= Greater than or equal to a >= b
<= Less than or equal to a <= b

Example:

def a = 10
def b = 3

println "Equal to: ${a == b}"      // Output: false
println "Not equal to: ${a != b}"  // Output: true
println "Greater than: ${a > b}"   // Output: true
println "Less than: ${a < b}"      // Output: false
println "Greater than or equal to: ${a >= b}" // Output: true
println "Less than or equal to: ${a <= b}"    // Output: false

  1. Logical Operators

Logical operators are used to combine multiple boolean expressions.

Operator Description Example
&& Logical AND a && b
` `
! Logical NOT !a

Example:

def a = true
def b = false

println "Logical AND: ${a && b}"   // Output: false
println "Logical OR: ${a || b}"    // Output: true
println "Logical NOT: ${!a}"       // Output: false

  1. Bitwise Operators

Bitwise operators are used to perform bit-level operations.

Operator Description Example
& Bitwise AND a & b
` ` Bitwise OR
^ Bitwise XOR a ^ b
~ Bitwise Complement ~a
<< Left Shift a << b
>> Right Shift a >> b
>>> Unsigned Right Shift a >>> b

Example:

def a = 5  // Binary: 0101
def b = 3  // Binary: 0011

println "Bitwise AND: ${a & b}"    // Output: 1 (Binary: 0001)
println "Bitwise OR: ${a | b}"     // Output: 7 (Binary: 0111)
println "Bitwise XOR: ${a ^ b}"    // Output: 6 (Binary: 0110)
println "Bitwise Complement: ${~a}"// Output: -6 (Binary: 1010)
println "Left Shift: ${a << 1}"    // Output: 10 (Binary: 1010)
println "Right Shift: ${a >> 1}"   // Output: 2 (Binary: 0010)
println "Unsigned Right Shift: ${a >>> 1}" // Output: 2 (Binary: 0010)

  1. Assignment Operators

Assignment operators are used to assign values to variables.

Operator Description Example
= Assign a = b
+= Add and assign a += b
-= Subtract and assign a -= b
*= Multiply and assign a *= b
/= Divide and assign a /= b
%= Modulus and assign a %= b

Example:

def a = 10
def b = 3

a += b
println "Add and assign: ${a}"     // Output: 13

a -= b
println "Subtract and assign: ${a}"// Output: 10

a *= b
println "Multiply and assign: ${a}"// Output: 30

a /= b
println "Divide and assign: ${a}"  // Output: 10.0

a %= b
println "Modulus and assign: ${a}" // Output: 1.0

  1. Other Operators

Groovy also supports some additional operators that are useful in various contexts.

Operator Description Example
?. Safe Navigation a?.b
?: Elvis Operator a ?: b
as Type Casting a as Type
instanceof Type Check a instanceof Type

Example:

def a = null
def b = "Hello"

println "Safe Navigation: ${a?.length}" // Output: null
println "Elvis Operator: ${a ?: b}"     // Output: Hello

def c = 123
println "Type Casting: ${c as String}"  // Output: 123

println "Type Check: ${b instanceof String}" // Output: true

Practical Exercises

Exercise 1: Arithmetic Operations

Write a Groovy script that takes two numbers as input and performs all arithmetic operations on them.

Solution:

def a = 15
def b = 4

println "Addition: ${a + b}"
println "Subtraction: ${a - b}"
println "Multiplication: ${a * b}"
println "Division: ${a / b}"
println "Modulus: ${a % b}"

Exercise 2: Relational and Logical Operations

Write a Groovy script that compares two numbers and prints the results of relational and logical operations.

Solution:

def a = 10
def b = 20

println "Equal to: ${a == b}"
println "Not equal to: ${a != b}"
println "Greater than: ${a > b}"
println "Less than: ${a < b}"
println "Greater than or equal to: ${a >= b}"
println "Less than or equal to: ${a <= b}"

def x = true
def y = false

println "Logical AND: ${x && y}"
println "Logical OR: ${x || y}"
println "Logical NOT: ${!x}"

Exercise 3: Bitwise Operations

Write a Groovy script that performs bitwise operations on two numbers.

Solution:

def a = 5  // Binary: 0101
def b = 3  // Binary: 0011

println "Bitwise AND: ${a & b}"
println "Bitwise OR: ${a | b}"
println "Bitwise XOR: ${a ^ b}"
println "Bitwise Complement: ${~a}"
println "Left Shift: ${a << 1}"
println "Right Shift: ${a >> 1}"
println "Unsigned Right Shift: ${a >>> 1}"

Conclusion

In this section, we covered the various types of operators available in Groovy, including arithmetic, relational, logical, bitwise, assignment, and other special operators. Understanding these operators is essential for performing different operations on data and writing effective Groovy scripts. In the next section, we will delve into control structures, which will help you control the flow of your Groovy programs.

© Copyright 2024. All rights reserved