Java is Groovy and Groovy is Java

by Stii

groovy_transparentgroovy |ˈgroōvē|
adjective ( groovier , grooviest ) informal dated or humorous
fashionable and exciting : sporting a groovy new haircut.
enjoyable and excellent : he played all the remarkably groovy guitar parts himself.

That is as defined in my beloved and trusted Mac Dictionary. I have to do a little Java project soon. Not because I want to, because I HAVE to for Afrigator. Don’t get me wrong, I think Java is great and powerful. I have a healthy dose of respect for it. Problem is, I’ve never done anything remotely formal in Java. The only experience I have with it was when I checked it out about 6 years ago. Safe to say, I’m a n00b.

The problem is that I’m quite busy these days. Time and energy is not much for attempting learning a mammoth like Java.

Hello, Groovy! Groovy is a dynamic language specifically for the Java platform. Groovy is built for the JVM, making it easy for Java to integrate with Groovy vice versa. In Netbeans, they went a far as not even having an option to create a Groovy project. You create a normal Java project and then add Groovy classes to it. That is how tightly it seems integrated, which is good news for a n00b like me :)

The syntax itself seems very close to that of Ruby. Lets look at these examples (from the excellent article by Gerald Bauer – Groooooovy Babe: Jazzing Up Plain Old Java)

Here is some Java code

import java.util.*;

public class HelloWorld
{
  public static void main( String args[] )
  {
     List country = new ArrayList();
     country.add( "Canada" );
     country.add( "Austria" );
     country.add( "Brazil" );

     Collections.sort( country );

     for( Iterator it = country.iterator(); it.hasNext() )
        System.out.println( "Hello " + it.next() );
  }
}

The same in Groovy ( note the code reduction compared to Java :) )

country = [ 'Canada', 'Austria', 'Brazil' ]
country.sort
country.each { println "Hello ${it}" }

And in Ruby

country = [ 'Canada', 'Austria', 'Brazil' ]
country.sort
country.each { |country| puts "Hello #{country}" }

To top it all off, they even have their own Rails-like web framework called Grails (Groovy on Rails). I’ll check that out in due time.

All in all, Groovy feels much more comfortable and familiar to me. I’m looking forward to this little challenge and will probably write about it as I go along. Aslam Khan assured me that it is solid and he even have some Groovy code out there in the wild in production which is comforting! That and knowing someone who is an expert to ask for advice when the going gets rough, since I’m fairly sure it will get rough due to n00bn3ss!