Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

The syntax of the Groovy language is derived from Java, but is enhanced with specific constructs for Groovy, and allows certain simplifications. Groovy allows you to leave out some elements of syntax like package prefix, parentheses, and semicolon which brings the code to the bare minimum. 

On this page:

Table of Contents

Comments in Groovy

Groovy supports single and multi-line comments. Single line comments start with // and can be found at any position in the line. Multi-line comments start with /* and */

...

Semicolons are used to separate statements, but they are optional in Groovy as long as you write one statement per line. If you use multiple statements on the same line you can either use a semicolon or a newline to separate the statements.

Code Block
languagegroovy
linenumberstrue
def A = "a" + "b"; def B = "c" + "d" // You need a semi-colon to separate the two statements on the same line

//You can instead add a new line to separate the statements in Groovy
def A = "a" + "b"
def B = "c" + "d

Return statement

The last line of a method in Groovy is automatically considered as the return statement. For this reason, an explicit return statement can be left out. To return a value that is not on the last line, the return statement has to be declared explicitly.

Code Block
languagegroovy
linenumberstrue
def a = 3 + 2 //returns 5. A return statement can be omitted

...