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:
- Download the latest version of Groovy from http://groovy.codehaus.org/Download.
- Once downloaded, extract the content of the zip file into your location of choice e.g. C:\groovy-2.0.1
- Create an environment variable called GROOVY_HOME and set it to C:\groovy-2.0.1
- Append %GROOVY_HOME%/bin to the path environment variable.
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 :
- Default visibility for methods and fields in Groovy is public.
- It is a dynamic language so type information is optional.
- Semicolon is optional at the end of a statement.
- Paranthesis is optional for top level expression.
- 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!");
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:
- 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!"
- 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"
that's it in this blog. In my next blog i will do a deep dive on some of the Groovy feature.
Thanks for blogging about Groovy. I subscribed to your feed.
ReplyDelete