HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to GroovyVariablesData TypesOperatorsStringsListsConditions (if / else)Loops (for / while)Functions
REFERENCE
📖 Overview
🖥️ Output
📦 Variables & Types
🔢 Numbers & Math
✍️ String
📚 List & Map
🔀 Condition & Loop
🧩 Function
Groovy›Operators

Operators

Arithmetic (+ - * / intdiv % **), comparison, and logical (&& || !) operators.

Simple enough for a 5-year-old to understand

Math operators

7/2 gives a float (3.5). Use intdiv() for integer division. ** calculates the exponent.

Example code
1
println 7 + 3
10
Output / Input10
2
println 7 / 2
3.5 (decimal)
Output / Input3.5 (decimal)
3
println 7.intdiv(2)
3 (integer division)
Output / Input3 (integer division)
4
println 7 % 2
1 (remainder)
Output / Input1 (remainder)
5
println 2 ** 10
1024 (power)
Output / Input1024 (power)
Output / Input
💡 Hint: Integer division intdiv(); exponent **; remainder %.

🎯 Exercise & Practice

Test what you've learned — answer, write code, check instantly. (1 left)

1.Print the remainder of dividing 23 by 4.

🎯 Expected output: 3

Loading editor…

About this topic

Groovy's operators cover those in Java and add a few useful additions on top. The most important difference is in the equality operator: in Groovy, this operator compares the contents of objects, rather than looking at reference identity like in Java.

This single difference can cause code copied from Java to silently behave differently. Groovy has a separate method for identity comparison, which is rarely needed.

Groovy-specific operators significantly shorten the code: safe navigation reduces null checks, the Elvis operator simplifies assigning default values, and the spread operator collects the same property from all elements of a collection into a single sign.

What you will learn

  • The behavior of the equality operator
  • The safe navigation operator
  • The Elvis operator
  • The spread operator
  • The comparison operator

Who is this for?

  • developers transitioning from Java
  • Those who want to shorten null checks
  • Script writers working with collections

When to use?

For configuration scripts, the Elvis operator is the shortest way to assign defaults to potentially missing values.

When you need to collect a single field from a list of objects, the spread operator makes writing loops unnecessary.

Common mistakes

Wrong

String a = new String("hello")
String b = new String("hello")
println a.equals(b)
println a == b

There is no error here, but Java knowledge is misleading: in Groovy, the equality operator compares contents and both lines return true. In Java, the second line would return false. This difference causes a change in behavior when porting Java code to Groovy.

Correct

String a = new String("hello")
String b = new String("hello")
println a == b
println a.is(b)

Wrong

def ayar = [:]
def deger = ayar.renk
if (deger == null) {
  deger = "blue"
}
println deger

There is no error here, but Groovy offers a much shorter operator for this pattern. A four-line null check reduces to a single expression and readability increases; this pattern is repeated dozens of times in configuration scripts.

Correct

def ayar = [:]
def deger = ayar.renk ?: "blue"
println deger

Wrong

def kisiler = [[ad: "Ali"], [ad: "Ayse"]]
def adlar = []
for (k in kisiler) {
  adlar << k.ad
}
println adlar

There is no error here, but Groovy's spread operator reduces this entire loop to a single expression. Collecting the same property from every element of a collection is a very frequently needed operation, and the language directly supports it.

Correct

def kisiler = [[ad: "Ali"], [ad: "Ayse"]]
println kisiler*.ad

Frequently asked questions

How is identity comparison done?

The object identity testing method is used. In Groovy, this is rarely needed because content comparison is usually desired; looking for identity comparison out of habit from Java is generally unnecessary.

How to use the Elvis operator and safe navigation together?

You use safe navigation at each step of the chain and add a default value at the end with the Elvis operator. This way, even if there is a null value anywhere in the chain, the result remains defined.

What does the comparison operator do?

It compares two values and returns negative one if smaller, zero if equal, and one if larger. It significantly shortens and makes the code readable when writing sorting comparators.

Summary

The equality operator compares the content.

Identity comparison is done with a separate method.

Safe navigation prevents null errors.

The Elvis operator provides a default value.

The spread operator collects fields from a collection.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code