Free preview.You're sampling one lesson — enroll free to unlock all 7 lessons and track your progress.
Enroll free
lesson

Variables and Logic

Variables and Logic

In this lesson — part of Scripting Basics — you'll learn variables and logic in Groovy and why it matters in real work.

Why it matters

Variables are how programs remember values — the foundation of every computation.

Key ideas

  • A variable is a name bound to a value
  • Choose clear, descriptive names
  • Constants vs. reassignable variables
  • Values have types

In practice

Here's how it looks in idiomatic Groovy:

def name = 'Groovy'          // dynamic type, inferred
String lang = 'JVM'          // optional static type
int count = 3

// def vs typed: 'def' fields/locals become Object
def list = [name, lang, count]
println list.class.name      // java.util.ArrayList
println "$name on the $lang, x$count"

Groovy note: 'def' is not a type but a placeholder that erases to Object, so a 'def'-typed variable silently skips the compile-time checks a declared type (String, int) would enforce.

Try it yourself

Exercise: Declare three variables in Groovy (a name, an age, and whether you're learning) and print them.

Recap

You now understand variables and logic and can apply it in Groovy. Mark this lesson complete and continue to the next one.