Home | Games | Media | Programming | Raspberry Pi | Misc Content


After coding Java for quite some time, I recently had the refreshing experience of coding in Groovy. It is fairly easy to transition to Groovy after coding in Java, and best of all compiled Groovy code will run using the same JVM that you use for Java.

What is Groovy

Basic Hello Associates Comparison

Java code has a lot of rules and syntax that may not always be needed from a specification standpoint in order to accomplish our goals. For instance, why do we need so many of the following when we code?

Compare the following examples that print Hello Associates to the screen in both languages.

Java Implementation of Hello Associate

public class Example { public static void main(String[] args) { System.out.println("Hello Associates!"); } }

Groovy Implementation of Hello Associate

println "Hello Associates!"

Transfer/Data Object Comparison

Groovy provides a number of mechanisms to assist in making your code more readable. A few of these are as follows.

Please feel free to look at the following application code that creates three objects and prints a comparison of the objects. The Groovy implementation definitely has some benefits of enabling a servicing developer to troubleshoot fewer lines of code if a problem is to be found.

Java Implementation of a Transfer Object

public class AllowedValue { public static void main(String[] args) { AllowedValueTO firstTO = new AllowedValueTO("12", "Auto"); AllowedValueTO secondTO = new AllowedValueTO("12", "Auto"); AllowedValueTO thirdTO = new AllowedValueTO("13", "Auto "); System.out.println(firstTO.equals(secondTO)); System.out.println(firstTO.equals(thirdTO)); } }

class AllowedValueTO { private String key; private String value;

public AllowedValueTO(String key, String value) {
	this.key = key;
	this.value = value;

public String getKey() {
	return key;

public String getValue() {
	return value;
} } </code>

Groovy Implementation of a Transfer Object

firstTO = new AllowedValueTO("12","Auto") secondTO = new AllowedValueTO("12","Auto") thirdTO = new AllowedValueTO("13","Auto ") println firstTO == secondTO println firstTO == thirdTO

@Immutable class AllowedValueTO { String key String value } </code>

A More Complex Code Example Comparison

Groovy can provide simplifications beyond defining simple objects, comparing them, and printing them. Let’s now look at an a collection of objects.

Groovy allows us to remove much of the noise found in Java and other languages. For example, one can do the following in Groovy.

Java Implementation of a Client List

import java.util.ArrayList; import java.util.List;

public class ClientList {

private List<String> clientIds;

public ClientList() {
	clientIds = new ArrayList<String>();

public static void main(String[] args) {
	ClientList cList = new ClientList();
     System.out.print(" "+cList.clientIds.getClass()+" : ");

public boolean add(Object obj) {
	return clientIds.add((String)obj);

public String toString() {
	return ""+clientIds.getClass() + " : "+clientIds.toString();
} } </code>

Groovy Implementation of a Client List

class ClientList { @Delegate List clientIds = []

static main(args) {
	def cList = new ClientList();
	cList.add "CLIENT_ID_1"
	cList.add "CLIENT_ID_2"
	println ""+cList.clientIds.class +" : "+cList
} } </code>

Additional Groovy Features

Groovy also provides the following nifty features!

====Additional Resources==== For more information, please feel free to consult any of the following resources.