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›Data Types

Data Types

Number, string, and boolean types; GString interpolation ($) and type conversion.

Simple enough for a 5-year-old to understand

Interpolation (GString)

You embed values into text using $variable or ${expression} within double quotes.

Example code
1
def name = "Alex"
2
def age = 36
3
println "$name $age years old"
Alex is 36 years old
Output / InputAlex is 36 years old
4
println "Future: ${age + 1}"
Curly braces for expressions.
Output / InputCurly braces for expressions.
Output / Input
💡 Hint: Use $variable directly; use ${...} for expressions.

About this topic

In Groovy, every value is an object; primitive types in Java are automatically wrapped where they are used. This allows you to call methods directly on a number.

Groovy's most surprising type decision concerns floating-point literals: a decimal value you write in the source code becomes a decimal precision object, not a float. This prevents rounding errors in financial calculations from the start, but surprises those coming from Java.

Division is also different from Java. Dividing two integers yields a decimal result; the fractional part is not truncated as it is in Java. If you want integer division, you must use a method that explicitly specifies it.

What you will learn

  • Basic types
  • The type of decimal literals
  • Division behavior
  • Type conversions
  • null value checking

Who is this for?

  • developers transitioning from Java
  • Script writers doing financial calculations
  • Those surprised by the division result

When to use?

Groovy's default decimal type is an advantage in money and rate calculations; you get accurate results without doing anything extra.

When interacting with Java libraries, you need to pay attention to type conversions; Groovy's defaults and Java's expectations do not always match.

Common mistakes

Wrong

int sonuc = 7 / 2
println sonuc

In Groovy, dividing two integers yields a decimal result; assigning this value to an integer variable silently rounds it or produces an unexpected result. If you want Java's truncation behavior, you must use the method that explicitly specifies it.

Correct

int sonuc = 7.intdiv(2)
println sonuc

Wrong

def a = 0.1
def b = 0.2
println (a + b == 0.3)

Here the result comes out correct and this is surprising: Groovy stores decimal literals as exact decimal types, so the classic floating-point error does not occur. But if you move the same code to Java, the result will be wrong. It is important to know that this behavior is language-specific.

Correct

double a = 0.1
double b = 0.2
println (Math.abs((a + b) - 0.3) < 1e-9)

Wrong

def kullanici = null
println kullanici.ad

Accessing a property of a null object throws a NullPointerException. Groovy offers the safe navigation operator for this: if the value is null, the expression returns null without throwing an error. In dynamic code, this operator is an almost mandatory habit.

Correct

def kullanici = null
println kullanici?.ad

Frequently asked questions

Why are decimal literals of exact type?

Because Groovy's initial target audience was business applications, and money calculations are critical there. Choosing an exact decimal type instead of floating-point eliminates the most common calculation error by default.

What does the safe navigation operator do?

If the value on the left is null, it doesn't evaluate the expression at all and returns null; if it's present, it accesses it normally. It can be chained, so even if any step of a long access chain is null, the program won't crash.

How do I convert text to a number?

Text objects have conversion methods and can be called directly. They throw an error on invalid input; it is safer to use the method that tests whether it's a number first on user input.

Summary

Every value is an object.

Decimal literals are of exact decimal type.

Division of two integers yields a decimal result.

Integer division is done with a separate method.

The safe navigation operator prevents null value errors.

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