JN0025-Starting Add comment to Wiki View in Wiki Edit Wiki page Printable Version

The Groovy Programming Language runs on top of the Java Runtime Environment, which itself runs on almost any computer system, such as Windows, Linux, and Macintosh. If you don't have Groovy installed, see these pages:

    Installing Groovy

    Running Groovy

If you don't have the Java Runtime Environment:

    Installing Java

These tutorials for those new to both Java and Groovy are in a sequence that builds on knowledge already presented. This tutorial therefore starts with the basics. Throughout, we use code examples rather than lengthy explanations to present the features of Groovy, so you might miss things if you just skim. We don't (yet) explain what you would use the features for, but rely on your previous programming background for this.

The code snippets in these tutorials use comments to explain things:

Groovy code can contain strings:

Each line here does the same:

We can also assign integers and decimals to variables:

We can use operators like + - * / and parentheses ( ) with numbers, following usual math grouping rules:

We can use the operators == > < >= <= != with numbers, the values true and false, the operators ! (not), && (and), and || (or), all with parentheses, to produce boolean expressions:

Variables are versatile:

All names in Groovy, including variable names, can contain any alphabetic character or the underscore, and contain any digit not in first position:

All data in Groovy is built from "classes" and instances of them. Class names by convention begin with an uppercase character:

We can inspect the class of any entity, such as numbers and strings, using the class field:

There are many predefined classes in Groovy, but only the most common ones are always visible to Groovy code. Most need to be qualified with a "package" name, eg, 'java.text.DecimalFormat', or the package must be imported beforehand:


If a line can be interpreted as a valid statement, it will be:

Sometimes code in a script doesn't compile: we comment it out in our examples. Other code compiles but generates a "checked exception" which we can catch and handle:

We can use square brackets [ ] to represent both ordered lists and key mappings:

We can specify a list as a 'range', ie, by only the first and last items:

We can convert data of one type to another using the 'as' keyword:

Sometimes, we need to use a more efficient type of list known as an array, where the type of each element must be the same. Arrays can't be represented directly in the syntax, but we can convert a list to one easily:

We can choose between two execution options using the if-else-statement:

We can execute some code a certain number of times:

We can enclose code in parentheses and execute it later. The enclosed code is called a "closable block" or "closure":

We can spawn new threads from our main thread:

After, say, 5 seconds, abort the program then look at the file. On many computers, it'll show a roughly equal distribution of 'S' and 'M', but there'll be some irregularities showing that thread scheduling isn't perfectly timed.

The tutorials following are grouped into functional areas, beginning with numeric processing, and build up to the advanced features of Groovy.



Results of your search request can come from various sources: the Groovy website itself, the JIRA issues, the API documentation, as well as a few other interesting Groovy-related blogs.

  By  -  pages  -  views  - last modified