Skip to content
Groovy 10 - Comments and Key Words

Comments

Comments are part of the code we write that will be ignored by the Groovy console. They are used to explain the code so that others or even ourselves can understand it later. There are two types of comments:

  • Single Line
  • Multi-line

Single Line Comments

Single line comments are done using double slash //

Example:

  1. def x = 1
  2. def z = 2
  3. // This part will be ignored by the console
  4. def y = x + z

Multi-line Comments

Multi-line comments can be written on multiple lines and are enclosed between /* and */

Example:

  1. def a = 3
  2. /*
  3. This is a
  4. comment on
  5. multiple
  6. lines
  7. */
  8. def c = a + 7

Key Words

These words are reserved in Groovy, meaning they cannot be used as variable names or for other purposes except for what they are assigned to.

Here is a table of these keywords:

Groovy Keywords

Source: Groovy Syntax

This means these words cannot be used, for example, as variable names:

def new = 11

Doing this will give an error when trying to save your script. But if you do:

def random = new Random()

This will not give you an error, as new keyword is used in the way it was designed to be used in Groovy.