Sunday, September 2, 2012

Say hello to Groovy - Part 1

With this blog, I am starting a series on Groovy. Chances are that you are already aware of what Groovy is but in case you are not; Groovy is an object oriented programming language.

Groovy is one of many JVM languages that compiles to bytecode that runs on the JVM. Groovy is also a dynamic language so it’s not statically typed like Java however it has a Java like syntax.It's also a scripting language so you don't necessarily have to compile the groovy code before executing it.


So why would you choose Groovy over Java or other dynamic languages that runs on the JVM like JRuby? While Java like syntax is definitely a plus, Groovy is designed to seamlessly integrate with Java i.e. you can implement a Java interface using Groovy and vice versa.
One other advantage is that Groovy has many modern programming features which were missing in Java e.g. Closure. If you are a Java developer, learning Groovy would be a cakewalk!

 
Getting Started
In order to get started, perform following steps:
  1. Download the latest version of Groovy from http://groovy.codehaus.org/Download.
  2. Once downloaded, extract the content of the zip file into your location of choice e.g. C:\groovy-2.0.1
  3. Create an environment variable called GROOVY_HOME and set it to C:\groovy-2.0.1
  4. Append  %GROOVY_HOME%/bin to the path environment variable.
Once you are done open a command prompt and type groovy - version. If Groovy's version is displayed in the command prompt you are all set to play with it!

 
Walkthrough of Groovy tools

Now that you have Groovy installed, lets look at the tools that you would need to play with it.

groovy - Use this tool to execute groovy source code (.groovy files).  This is typically used for executing Groovy scripts.

groovyc - Use this tool to compile groovy code to Java class file. You would use this tool so that you Groovy source code is not compiled every time before execution.

groovyConsole - This is a minimal editor that you can use to load, type and exceute Groovy code.



 
Typical Hello World

Introduction to any programming language is incomplete without a Hello World program so lets see how a hello world program looks like in Groovy.While we are at it, we will also see how Groovy cuts down on a lot of boilerplate code from Java. Here is how a typical Hello World program would look like in Java.


Here is the list of Groovy features that we are going to leverage :
  1. Default visibility for methods and fields in Groovy is public.
  2. It is a dynamic language so type information is optional.
  3. Semicolon is optional at the end of a statement.
  4. Paranthesis is optional for top level expression.
  5. Groovy provides println which can be used in place of System.out.println. You can use println "Hello World!" inplace of System.out.println("Hello World!");
Once we factor in all the above mentioned features, this is how a Hello World program would look like in Groovy.

Ok. I also mentioned that Groovy is a scripting language so it automatically wraps a script with a default class and main method. So your hello world program in Groovy can actually be a single line of code.



Enhanced Hello World

Now that you got an idea of how Groovy cuts down the boilerplate code and enhances developer productivity, lets look at an enhanced Hello World program.

Here is the enhanced version of Hello World code in Java



lets leverage some of the Groovy features mentioned below and rewrite the same program in Groovy:
  1. Groovy has a feature called Gstring which allows you to write a string containing an expression that can be evaluated. e.g. instead of "Hello "+getName()+"!" you can use something like "Hello $name!"
  2. It also has something called Groovy Bean i.e. if you have a public field or property in a bean class, Groovy automatically generates getter and setter for it. Groovy also provides shortcut to access and set these properties. e.g. in the above example, instead of hello.setName("world") you can use hello.name = "World"
Ok. So this how it will look in Groovy.

 


that's it in this blog. In my next blog i will do a deep dive on some of the Groovy feature.

1 comment:

  1. Thanks for blogging about Groovy. I subscribed to your feed.

    ReplyDelete