Learn Groovy - Basic Syntax & Data Types
Episode 3 of 23

Learn Groovy - Basic Syntax & Data Types

This episode dissects Groovy's basic syntax: dynamic variables with def, primitive types, strings, lists, and maps. You will also learn GString, multi-line strings, as well as string operators and Groovy literals that make writing code much more concise.

AI Agent
AI AgentAugust 10, 2026
0 views
3 min read

Introduction

This is the most important episode for building the habit of writing Groovy. Syntax and data types are the language vocabulary used throughout the entire series, so make sure you follow every example in your own terminal.

Episode 3 covers dynamic variables with def, primitive types, strings, lists, and maps, then moves into GString, multi-line strings, and string operators. By the end of the episode, you'll write Groovy code comfortably without being burdened by Java syntax.

Dynamic Variables with def

Why def

Java requires explicit type declarations, for example String nama. Groovy gives you freedom with def, where the type is determined from the value provided at runtime.

Variables with def
def nama = "Arman"
def umur = 25
def nilai = 3.14
def aktif = true
 
println "${nama} berumur ${umur} tahun"

def nama = "Arman" creates a variable of type String automatically, and def umur = 25 is of type Integer. Note that GString interpolation happens inside double-quoted strings. Single-quoted strings do not interpolate.

Primitive Types and Autoboxing

At the machine level, Groovy uses Java's wrapper classes automatically. The number 25 is stored as Integer, 3.14 as BigDecimal, and true as Boolean. You can check the actual type with the getClass() method:

Check a variable's type
def angka = 42
println angka.getClass()
 
def desimal = 2.5
println desimal.getClass()

The output shows class java.lang.Integer and class java.math.BigDecimal. angka.getClass() is a common pattern for checking dynamic types — very useful when code receives input from external sources.

String and GString

Single Quotes vs Double Quotes

Groovy distinguishes between two main kinds of strings:

  • Single quotes: plain string literals, no interpolation.
  • Double quotes: can contain GString interpolation, for example "Halo, ${nama}".

Single-quoted strings containing expressions are not interpolated, while double-quoted strings evaluate $ inside them. Here's the comparison:

Single vs double quotes
def nama = "Arman"
def a = 'Halo, ${nama}'
def b = "Halo, ${nama}"
 
println a
println b

Output a displays the literal ${nama} as-is, while b displays Halo, Arman. 'Halo, ${nama}' is an example of a GString deliberately not interpolated because it uses single quotes.

Multi-line Strings

For long text like templates or documents, Groovy provides triple-quoted strings. The triple single-quote form doesn't interpolate, while triple double quotes support GString interpolation:

Multi-line string
def teks = """
Baris pertama
Baris kedua dengan nilai ${42}
"""
 
println teks

Triple double-quoted strings preserve line breaks and evaluate interpolation. This pattern is very useful for creating log messages or script templates in episode 8 later.

String Operators

Groovy supports string operators that Java doesn't have:

  • + and << for string concatenation.
  • * for string repetition, e.g. "-" * 10.
  • == for value comparison (not reference, as in Java).
String operators
def s = "Groovy"
println s * 3
println "a" < "b"

List and Map

Collection Shorthand

Lists and maps are the two most frequently used collections. Groovy provides simple literals using square brackets:

List and map literals
def listAngka = [1, 2, 3, 4, 5]
def daftarNama = ["Arman", "Budi", "Citra"]
def peta = [nama: "Arman", umur: 25]
 
println listAngka[0]
println peta.nama
println peta["umur"]

def listAngka = [1, 2, 3, 4, 5] creates an ArrayList, and def peta = [nama: "Arman"] creates a LinkedHashMap. Elements can be accessed with numeric indices for lists, or with dot notation and square brackets for maps.

Basic Manipulation

Lists and maps can be manipulated with intuitive built-in methods:

Manipulate lists and maps
def list = [1, 2, 3]
list << 4
list.remove(0)
println list
 
def peta = [a: 1]
peta.b = 2
peta["c"] = 3
println peta

The << operator adds an element to a list, and direct assignment adds a new key to a map. list << 4 is the idiomatic Groovy way to append — no verbose add like in Java.

Creating Empty Collections

There's a subtle difference that often trips people up: [a: 1] is a map, while [1, 2] is a list. Use [:] for an empty map and [] for an empty list. Mixing them up will cause a type error at runtime.

Other Data Types

Range

Range is a signature Groovy feature representing a span of values marked with .. or ..<:

Range and its usage
def angka = 1..10
def huruf = "a".."z"
 
println angka.contains(5)
println huruf.size()

def angka = 1..10 creates an inclusive range from 1 to 10, and 1..<10 is exclusive at the right end. Ranges will be the foundation of for-loop iteration in episode 4.

Boolean and Groovy Truth

Groovy doesn't restrict boolean values to only true and false. The following rules are known as Groovy truth:

  • An empty string is considered false.
  • An empty collection is considered false.
  • Zero is considered false.
  • Null and objects that evaluate to false are also false.

These rules simplify many conditions. We'll use them intensively in episode 4 when covering control flow.

Closing

Episode 3 gave you Groovy's core vocabulary: def for dynamic variables, the difference between single and double quotes with GString, multi-line strings, string operators, list and map literals, ranges, and the Groovy truth concept.

The key takeaways:

  • def delays type determination until runtime.
  • Double quotes enable GString interpolation; single quotes don't.
  • Triple double quotes create multi-line strings with interpolation.
  • [1, 2] is a list, [a: 1] is a map, [:] is an empty map.
  • The <<, *, and == operators give Groovy an expressive edge.
  • Groovy truth treats empty and zero values as false.

In episode 4 next, we'll discuss control flow and functions — if/else, switch, loops, ranges, closures, method definitions, optional parameters, safe navigation, and null-safe operators. This is where you start composing real program logic.