Learn/Groovy/1. Hello, output
Lesson 1 of 8
Sign in

Groovy, lesson 1

Hello, output

Groovy is a language for the Java Virtual Machine. It can use any Java library, but it trims away most of Java's ceremony: no class or main method is needed for a simple program, semicolons are optional, and so are many parentheses. Here is a complete Groovy program:

println prints a value and then moves to a new line. Text in quotes is a string. You may write the call with parentheses, println("Hello, World!"), but when a call has arguments and stands on its own line, Groovy lets you leave them out.

Printing without a new line

print prints its value and stays on the same line, so the next output continues right after it:

println with no argument just ends the current line.

Strings with placeholders

A double-quoted string can contain ${...}. Groovy evaluates the expression inside the braces and inserts its value. This is called a (Groovy string):

2 exercises to try what you just read.

Exercise 1

Your first program

Press Ctrl Enter or Check to test your code.

Exercise 2

Launch report

Press Ctrl Enter or Check to test your code.

GString

Single-quoted strings, like 'Hello', are plain text: ${} is not expanded inside them. Use them when you do not need placeholders.

For precise layouts, printf takes a format string with %s for text, %d for whole numbers and %.2f for decimals with two places. End it with %n to get a new line:

Comments

// starts a comment that runs to the end of the line. /* ... */ can span several lines.

How a script runs

A Groovy file without a class is a script. Groovy compiles it to Java bytecode and then runs the statements one after another, from the top of the file to the bottom. If the code cannot be compiled, you get an error with a line number, and nothing runs. If something goes wrong while it runs, you get an exception and a stack trace; look for the line that mentions your script to find where. Starting the Java virtual machine takes a moment, so even tiny scripts need about a second.

Further reading

  • Groovy syntax: comments and strings
  • Program structure: scripts versus classes

Write a program that prints exactly these two lines:

The program reads no input.

Print a short launch report. Build the first line from three print calls that print 3..., 2... and 1..., followed by println "Liftoff!". For the second line, store the rocket name Comet and the crew size 4 in the variables provided and print them with one println and a ${} placeholder for each.

Expected output:

println "Hello, World!"
print "Loading"
print "..."
println "done"          // Loading...done
def language = "Groovy"
println "Hello from ${language}!"     // Hello from Groovy!
println "2 + 3 = ${2 + 3}"            // 2 + 3 = 5
println "Plain name works too: $language"
printf "%s has %d items%n", "Basket", 3
printf "Pi is about %.2f%n", 3.14159     // Pi is about 3.14
// A whole-line comment
println "This runs"   // a comment after code
/* A comment
   over two lines */
Hello, World!
I am learning Groovy.
3...2...1...Liftoff!
Comet launched with 4 crew.