Free tutorials for Java, Eclipse and Web programming



Follow me on twitter

Groovy with Eclipse - Tutorial

Lars Vogel

Version 1.4

08.02.2011

Revision History
Revision 0.112.11.2008Lars Vogel
Created
Revision 0.2 - 1.402.12.2008 - 08.02.2011Lars Vogel
bug fixes and enhancements

Groovy

This article gives a short overview of the Groovy language including collections, loops, gstrings, MOP, closures, operator overloading, XML handing and using Groovy together with Java class. It also describes how to use Eclipse for developing Groovy.

This article assumes that you have already Eclipse installed and that you have used Eclipse for Java development. This article was written using Groovy 1.7, Eclipse 3.6 (Helios) and Java 1.6.


Table of Contents

1. Groovy
1.1. Overview
1.2. Features
2. Installation
2.1. Groovy
2.2. Eclipse Plugin
3. First Groovy project
4. Groovy Classes, Objects and Methods
4.1. Groovy Classes
4.2. Classes and class variables
4.3. Equals, == and the method is()
4.4. Optional Parameters
5. Loops
6. Groovy Datatypes
6.1. Reference variables
6.2. Strings
6.3. Lists and maps
6.4. Ranges
7. Regular expressions
8. Closures
9. Meta Object Protocol
10. Operator overloading
11. Groovy and Files
12. Groovy and XML
13. Groovy and Concurrency
14. Example usage of Groovy
14.1. Groovy Classes and class variables
14.2. Safe Navigation Operator
14.3. Elvis operator
15. Grails
16. Using Groovy classes in Java
17. Using Groovy via the command line
17.1. The Groovy shell
17.2. The Groovy Console
17.3. Compile Groovy Classes
18. Thank you
19. Questions and Discussion
20. Links and Literature
20.1. Groovy Links

1. Groovy

1.1. Overview

Groovy is a dynamic language which is based on the Java Virtual machine. Groovy supports standard Java constructs including annotations, generics, static imports, enums, varargs and in addition advanced language features as

Groovy is a dynamic language that runs on the JVM and is tightly integrated with the Java language. Groovy provides lots of simplifications compared to standard Java language features and advanced language features as properties, closures, native support for lists, maps and regular expressions, duck typing and the elvis operator.

Groovy is almost compatible to Java, e.g. almost every Java construct is valid Groovy coding which makes the usage of Groovy for an experience Java programmer easy.

The following assumes that you have already Java programming experience and focus on Groovy specific features.

1.2. Features

While the simplicity and ease of use is the leading principle of Groovy here are a few nice features of Groovy:

  • Groovy allows to change classes and methods at runtime. For example if a class does not have a certain method and this method is called by another class the called class can decided what to do with this call.

  • Groovy does not require semicolons to separate commands if they are in different lines (separated by new-lines).

  • Groovy has list processing and regular expressions directly build into the language.

  • Groovy implements also the Builder Pattern which allows to create easily GUI's, XML Documents or Ant Tasks.

  • Asserts in Groovy will always be executed.