skip to Main Content

Groovy is a lightweight, object oriented, dynamic language designed to supplement Java development.  Running on the Java Virtual Machine (JVM), it can be used as a scripting language, or compiled down to bytecode.  An open source language under the Apache 2.0 license, Groovy gives you the power of advanced language features like dynamic methods, closures and Meta Object Programming (MOP). Most importantly, Groovy is designed as a companion to, not a replacement for, Java.
 

What is a dynamic language?

Dynamic languages can extend an application at runtime to change behaviors, types and objects.  With this functionality, it is possible to execute statements that are created at runtime.  A dynamic language can accomplish common programming behaviors that statically typed languages must do at compile time.

 

Why Groovy?

Groovy has familiar syntax for Java developers, while also including language features such as lists and maps as first class citizens (which you may be familiar with from languages like JavaScript).  It has a relatively flat learning curve, since anything you can do in Java you can do in Groovy, including the syntax.  Since it runs in the JVM of Java 5 and newer, it has a low barrier to entry.  Add the JAR to your classpath, and think of Groovy as a simple and powerful  extension of the JDK.

 

Why did Groovy come about?

James Stachan invented Groovy over a decade ago.  While he and his wife were traveling, waiting for a late airplane, James began reading about some of the features of Python. Hoping to bring functionality such as dynamic behaviors and common datatypes to the Java platform that he worked in everyday, James began to develop Groovy, taking cues from Ruby and Smalltalk, as well.

 

What makes Groovy different?

Groovy gives you the option to use semicolons, but if you want to leave them off, that’s okay too.  Additionally, the return statement is optional.  In Java you use .equals() to compare two objects and in Groovy you can use == to compare two objects.  With Groovy you do not need to specify a type for a primitive. Instead, you can simply use def:

def blah = “I am a String!”

def pi = 3.14

 

Of course, you can still do it this way:

String blah = “I am a String!”

float pi = 3.14

 

Who should use Groovy?

Since Groovy runs on the JVM and interoperates well with Java, Java developers would find Groovy to be a good option.  Also, Java code can run alongside Groovy code so the two can coexist nicely in projects.  Java developers new to the Groovy language can always fallback to coding in Java and then come back later to refactor the Java code to Groovy once they are more comfortable with the tool.

 

What does it look like?

The Groovy syntax should look familiar to Java developers, with some subtle differences.  Here is a comparison to create a new date.  In Java it would take two lines:

import java.util.*;

Date today = new Date();

 

In Groovy we can do this with just one line of code:

today = new Date()

Groovy doesn’t need to import java.util package to use the Date type.  Groovy imports java.util, java.io, java.net and many others by default.

 

Who is using Groovy?

Major corporations are using Groovy; they include:

  • Netflix
  • Best Buy
  • Target
  • IBM
  • Google
  • Oracle
  • Wells Fargo
  • LinkedIn
  • Cisco Systems

If you’re looking to expand your skill set while staying in familiar territory, give Groovy a try!

This Post Has 2 Comments

  1. Thank you for another wonferdul post. The place else may just anybody get that kind of information in such an ideal means of writing? I’ve a presentation subsequent week, and I’m on the search for such information.

  2. Thanks for every other informative blog. Where else could I get that kind of info weittrn in such an ideal approach? I have a challenge that I am simply now working on, and I have been on the look out for such information.

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top