Groovy from the ground up

Your first Groovy script

Printing a line, letting Groovy do the arithmetic, and the flat shape of a script that has no class and no main method.

Part 1 of 12

Beginner12 min readBack to Courses

A script, not a program

Groovy runs on the JVM, the same machine Java runs on, but it does not ask you to wrap your first line in a class. A file of statements is a script: Groovy compiles it in memory and runs it from the first line to the last. There is no main, no public static void, no file name that has to match a class name. The class the compiler generates takes the file’s name, which is why a stack trace from a script says main even though you never wrote a method called that.

The sandbox keeps your program in one file called main.groovy and runs it with the real Groovy runtime. Nothing reads input and nothing touches the network, so every lesson prints rather than asks — which is also why the output pane is the whole story of what a program did.

Printing, and letting Groovy calculate

println writes its argument and ends the line; print writes without ending it, so two prints run together. Giving println nothing at all — println() — writes an empty line, which is how a program puts space between two blocks of output.

The argument can be an expression and Groovy will work it out first: println 6 * 7 prints 42. Precedence is the arithmetic you learned at school — multiplication and division before addition and subtraction — and parentheses override it when you want something else. A quick check before running a line is to work it out on paper; when the printed number disagrees, the cause is either precedence or a type, and printing the parts one at a time tells you which.

Text next to numbers

The plus sign means two things. Given two numbers it adds them; given a string and anything else it joins them into text. That makes the order of a mixed expression matter: "six times seven is " + 6 * 7 works out the multiplication first, so the text is joined with 42, while "3 + 4 = " + 3 + 4 prints 3 + 4 = 34 because the joining happened before the sum.

The fix is brackets, because brackets are an instruction rather than decoration: "total: " + (3 + 4) adds first and joins the answer, printing total: 7. Every time a line mixes text and arithmetic, the arithmetic belongs in parentheses.

Printing and arithmetic

// println writes a line; print writes without ending it.
println 'Hello, Groovy!'
print 'no newline here'
println()

// Expressions can be printed directly.
println 6 * 7
println 10 / 4
println 'four plus four is ' + (4 + 4)

// A comment starts with // and runs to the end of the line.
def greeting = 'Hi'
println greeting + ', Ada!'

What the file becomes

The runner compiles main.groovy to a class in memory, then runs it. The statements at the top level become the body of a generated run() method, which is why they execute in order and why a variable declared there disappears when the script ends.

Methods you write in the script become methods on that class. That explains two things that otherwise look like magic: a script can define a method below the line that calls it, and a top-level variable cannot be seen inside that method — they belong to different scopes.

Output is the whole record

The output pane shows precisely what the program printed, in order, with nothing added. A line that did not appear is a line that did not run, so the first debugging move is to print the value you are unsure about rather than to guess at it.

When the compiler rejects the file, the message names a line and a reason. Read that line in your own file rather than trusting the number alone: the parser often notices a missing bracket later than it happened, so the real mistake can sit a line or two above the reported one.

Three habits cause most first-script confusion. print does not end the line, so two prints land on one line; println() is the empty line you wanted. A mixed expression such as 'total: ' + 3 + 4 joins as it goes and prints total: 34, not total: 7. And 10 / 4 is 2.5, not 2 — use 10.intdiv(4) when you mean the whole-number answer.

Try these next

  • Print a three-line receipt with println, then put the first two lines on one line using print.
  • Work out 2 + 3 * 4 and (2 + 3) * 4 on paper before printing both.
  • Add a comment above each line saying what you expect it to print, then run and compare.

Run it, then make it yours

Press Run and read the output line by line. Then change the greeting, print an expression of your own, and add a line that joins text with a sum in brackets.

Output
Click "Run" to execute your code

A receipt in six lines

Run it and check the total by hand. Then add a third item with its own quantity and price, and a line that prints the count of items using brackets around the arithmetic.

Output
Click "Run" to execute your code

Every run is a round trip to a compiler service, so the first press of Run takes a second or two longer than the output suggests. Run is disabled while a submission is in flight.

Check what you learned

One question at a time. A wrong answer explains itself — take another go.

Part 1 of 5

What does a Groovy script need before its first statement can run?