What Groovy is

Groovy is an object-oriented language for the Java platform. It compiles to the bytecode a Java compiler writes, so a Groovy class is an ordinary JVM class, Java libraries need no adapter, and a Java class can call a Groovy one without knowing it is Groovy. The language is dynamic by default: a variable can be untyped, a call is resolved at run time, and a script needs neither a class nor a main method.

A first script is one line, and the closures that Java added in version 8 have been in Groovy since 2007:

hello.groovy

def languages = ['Groovy', 'Java', 'Kotlin'] languages.each { name -> println "Hello, ${name}" }

groovy hello.groovy

Hello, Groovy
Hello, Java
Hello, Kotlin

Java underneath

A Groovy script runs on the same virtual machine as Java, with the same standard library. The script below builds a Java list, writes into it, and calls a Java static method; nothing in it belongs to Groovy except the missing types and the missing semicolons.

interop.groovy

List<String> names = new ArrayList<>() names.add('Groovy') names.add('Java') println String.join(' and ', names) println Integer.parseInt('42') + 1 println names.getClass().name

groovy interop.groovy

Groovy and Java
43
java.util.ArrayList

Where Groovy is used

Gradle build scripts are Groovy: a build.gradle file is a script that Gradle compiles and runs, and the Gradle course reads one line by line. Spock, the test framework of the JUnit platform, writes its specifications in Groovy. The Grails framework builds web applications with it, Jenkins pipelines are Groovy scripts in a Jenkinsfile, and SoapUI runs Groovy as the script language of its test steps.

The other use is scripting: a Java application embeds the Groovy runtime and lets a user extend it with a script, and a one-file program does a job that would otherwise need a build and a project layout.

What Groovy adds

Where to go next

The next lesson installs the language and reads the version line; Install Groovy also links the install pages of each system. The Groovy errors page collects the messages a beginner meets, and the Groovy DSL lesson of the Gradle course shows the language at work in a build.

Related

Автор статьи: Arthur Isaev