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.

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.
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.
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.
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:
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.
Groovy distinguishes between two main kinds of strings:
"Halo, ${nama}".Single-quoted strings containing expressions are not interpolated, while double-quoted strings evaluate $ inside them. Here's the comparison:
def nama = "Arman"
def a = 'Halo, ${nama}'
def b = "Halo, ${nama}"
println a
println bOutput 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.
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:
def teks = """
Baris pertama
Baris kedua dengan nilai ${42}
"""
println teksTriple 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.
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).def s = "Groovy"
println s * 3
println "a" < "b"Lists and maps are the two most frequently used collections. Groovy provides simple literals using square brackets:
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.
Lists and maps can be manipulated with intuitive built-in methods:
def list = [1, 2, 3]
list << 4
list.remove(0)
println list
def peta = [a: 1]
peta.b = 2
peta["c"] = 3
println petaThe << 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.
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.
Range is a signature Groovy feature representing a span of values marked with .. or ..<:
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.
Groovy doesn't restrict boolean values to only true and false. The following rules are known as Groovy truth:
false.false.false.false.These rules simplify many conditions. We'll use them intensively in episode 4 when covering control flow.
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.[1, 2] is a list, [a: 1] is a map, [:] is an empty map.<<, *, and == operators give Groovy an expressive edge.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.