Skip to content
AgileLogics
4 min read

Your First Program

Prerequisites: Installing Groovy

Create a file named hello.groovy:

println 'Hello, world!'
println 'Welcome to Groovy.'

Run it:

groovy hello.groovy

Expected output

Hello, world!
Welcome to Groovy.
  • println prints a value followed by a new line.
  • Parentheses around method arguments are optional when the call is a statement on its own: println 'hi' is the same as println('hi').
  • No class, no main method, no semicolons: a Groovy script runs from top to bottom.

The same program in Java style

Groovy also accepts the Java version — this file is valid Groovy too:

public class Hello {
    public static void main(String[] args) {
        System.out.println("Hello from Java-style Groovy!");
    }
}
Hello from Java-style Groovy!

That's why Java developers can start with what they know and drop the ceremony step by step.

print and printf

print 'Loading'
print '...'
println ' done'
printf('%s scored %d points%n', 'Ada', 42)
Loading... done
Ada scored 42 points

print doesn't add a new line; printf formats values with the same placeholders as Java's String.format (%s text, %d whole numbers, %n new line).

Practice

Write a script that prints your name on one line and your favorite food on the next.

Show solution
println 'Ada'
println 'Pizza'
Ada
Pizza