Monday, February 17, 2014

Groovy - The Groove of Java ( Groovy for Impatient)

Finally, after a long period of time, i started writing back...  I decided to give Groovy a chance, and it really bounced back with a bang as like always. Such a great language, which seems like not getting enough attraction as it should be. I would be say (if performance should not be the only barometer), then this may be a perfect replacement of Java.
Every feature provided by Groovy either turns out to be really nice crafted feature on top of the solid foundations provided by Java. I am going to write a series of posts on Groovy (especially for the impatient Java developers), so that they can add something sharp in their armory.

I am starting with the Java5 features and how they are implemented/Enhanced in Groovy. I will try to provide a basic set of examples that will work on your system without any problem. I am assuming that Groovy has been installed and GROOVY_HOME environment variable has been created with Groovy bin folder being presented in the PATH of the operating system.

  1. Autoboxing and Unboxing:
    As Groovy supports metaprogramming, groovy provides an automatic promotion/demotion of objects based upon the kind of usage. E.g. 
     int firstVariable = 100;  
     print firstVariable .getClass().name  
    This will going to print "java.lang.Integer" because of usage of the int is being like an object, so the primitive has been converted to object. Prior Groovy 2.0 primitives are being treated always as objects but going forward 2.0 further optimizations has been done to make intype conversion.
  2. For Each loop:  For-each loop which has been introduced in Java 5, needs us to declare the type of the Array/Collection, this constraint has been removed in Groovy.
      String[] stringAr = [ " Ram ", "Shyam ", "Manoj" ]  
       for(str in stringAr)  
    This will print " Ram Shyam Manoj" without any indication in FOR loop what is the TYPE of STR variable. Groovy identifies this at Runtime, thus providing a way to change the type of object at Runtime, it can be collection/array.
  3. Enum: Enums as like java can be used in Switch case, but Groovy provides a functionality to use multiple ENUM in a single case statement or a range of ENUM values. 
           NUM myNumber = NUM.THREE  
           switch(myNumber) {  
               case [NUM.ONE, NUM.TWO]:  
                    print "You have entered either 1 or 2 "   
               case NUM.ONE .. NUM.SEVEN:  
                    print "You entered somewhere between 1 and 7 "   
                default :  
                    print "I dint know what you have entered"  
    This will print "You entered somewhere between 1 and 7 ", which is indeed an enhancement over the way Java handles the Switch cases with ENUM.
  4. Variable Arguments:  Variable arguments can be provided in same way as that of java however instead of sum(int a, int... b) we can also use sum(int a, int[] b), where b represents an array of integers. 
  5. Annotations: Groovy supports all the annotations provided by Java, however it adds up a number of specific annotations which are very much useful keeping an eye over the Dynamic nature of language e.g. @TypeChecked. I will explore all the supported annotations in the coming posts. 
  6. Static Import: All Static variables/functions can be imported with an advantage that an ALIAS can be created for the long name of function/class.
    E.g import static Math.random as RND
    Now we can use RND() in place of random()
  7. Generics: Groovy fully supports generics of java with an added advantage, that the TYPE CHECKING of performing any operation can be delegated to runtime, and groovy is going to take care of conversion of the parameter to the type of generic which is declared.
  List<String> list = new ArrayList<String>();  
       print list  
 This code will print "Pankaj 5 5.4" while java wont be able to compile it. and Groovy adds the groovy and automatically find the best possible conversion to remove the error.

That's all for now, in the next post, we are going to cover the new set of annotations which are introduced in Groovy.