The TechGuy

September 8, 2012

Java to Groovy by Example – HelloWorld

Filed under: Framework/APIs,Groovy,Java,Scripting — Zeeshan Bilal @ 3:30 AM


Groovy is another language for Java Virtual Machine. It’s built on Java, seamlessly integrates with existing Java classes, and compiles to byte code. It’s Object-Oriented (even more where numbers are also objects), and supports the same programming model as Java. However, it’s also a dynamic language with additional powers like scripting and DSL. It reduces scaffolding code that makes it more readable, maintainable and increases productivity.

Groovy is very similar in syntax with java, so developers from java side can learn it easily. It also supports java syntax that means some of java programs can also run over groovy. To learn it we first look at a simple Hello World java program, and then we discuss line by line groovy versions of this java code.

Traditional Java Program

Let’s have a look at simple hello world java program,

  1. public class HelloWorld {
  2. public static void main(String[] args) {
  3. System.out.println("Hello World!");
  4. }
  5. }

HelloWorld is always used as very basic example to learn any programming language. We start with this to easily understandable class HelloWorld, which contains entry point method main. And it prints “Hello World!” on the program console. Now start migrating from this java code to groovy code/script.

First step to Groovy

As I mention earlier some of the java programs may also run over groovy without changing a single line of code. Exactly, this program can also run in groovy environment and it behaves same. Although, it can run over groovy but we can’t say it a groovy program. First of all we remove some scaffolding code to improve code readability.

If we look at the very first line of the java code, it’s a class declaration with public access modifier.

Class and all its methods are by default public in groovy.

So there is no need to explicitly specify public with class declaration or even with main method.

Semicolon is a very basic element in a program structure, in java semicolon is mandatory for statement termination.

Semicolons are optional in Groovy.

Note: But there are some situations where you need to use the semicolons in your groovy application for smooth execution.

For now we can remove all the semicolons in our program and it will work fine. Java is a statically typed language, whereas groovy is not that;

Groovy supports dynamic typing.

So we can drop argument’s type information from the main declaration.

Default return type is void

Based on this default behavior we can ignore void from main method and the signature of main would be just static main(args).

Lots of new helper methods are added to the groovy JDK. It gives handy methods and shortcuts for some routine tasks such as console printing. You can replace System.out.println() with just println(). At this level we are almost done with scaffolding and lets see how code looks like with these changes.

  1. class GreetWorld {
  2. static main(args) {
  3. println("Hello World!")
  4. }
  5. }

It’s really neat and handsome piece of code that is very easy to understand.

Wait for a minute

Groovy is a scripting language as it allows you to write some free form programs that don’t require you to define a class structure.  We may use these scripts in Unix shell, in our existing java code or just on command line.

We can optionally omit parentheses for top-level statements (a statement which is just a method call with some parameters). So at the end we leave main method back in java and use just the code as script in groovy.

Here it is, the smallest hello world example in groovy presented to you;

  1. println "Hello World!"

Wow! It’s really awesome; the groovyfied code is much more readable, succinct and crispy.

If we compile the above script to bytecode using groovyc, it will generate a single class named after the name of the script file. e.g. if this was saved in HelloWorld.script, a HelloWorld.class file will be the output just like java. When we execute this class, it execute the autogenerated main(String[] args) method in the bytecode.

Last but not Least

At the end we recap all in few points to summarize as a feature list that we learned in this post.

  • Class and all its methods are by default public.
  • Semicolons are optional.
  • Dynamic and static typing is supported
  • Default return type is void
  • Omit parentheses
  • Groovy is a scripting language

Where to go next

You can get more familiar with groovy, some advance concepts in the next part.

Next Page »

The Rubric Theme. Blog at