Thursday, April 29, 2010

GROOVY

                                                                                                                  -Dhanapathi


What’s Groovy:-


Groovy is a language similar to that of Java with an easier syntax and that which provides ability to write code the code on the fly helping the developers to develop the application and Unit test it at a greater pace. It acts as glue joining various modules and provides the feature of runtime (dynamic) type checking.
Groovy is a language that is tightly coupled to Java and that which runs on JVM. Groovy supports standard Java constructs including annotations, generics, static imports, enums, varargs etc. Thus the usage of Groovy is made easier for the Java developers. Groovy provides advanced language features as properties, closures, native support for lists, maps and regular expressions, duck typing and the elvis operator.

Surprising features:-

• Groovy's relaxed Java syntax allows you to drop semi-colons and modifiers.
• Groovy's syntax also permits you to drop a variable's type.
• Everything in Groovy is public unless you state otherwise.
• Asserts in Groovy will always be executed.
• Groovy allows changing classes and methods at runtime. For example if a class does not have a certain method and this method is called by another class the called class can decided what to do with this call.
• Groovy has list processing and regular expressions directly build into the language. In normal Java code, if you want to create a list of items, you first import java.util.ArrayList (for example), and then programmatically initialize an ArrayList instance, and then add items to it. In Groovy, lists and maps are built into the syntax — you don't need to import anything.
• Groovy implements also the Builder Pattern which allows creating easily GUI's, XML Documents or Ant Tasks.
• Anything you write in Groovy can be compiled into a normal Java class file and re-used in Java code. Likewise, anything you write in normal Java code can be reused in Groovy. As a result, you can easily use Groovy to write unit tests for Java code, for instance. And if you write a handy utility in Groovy, you can also use that utility in your Java programs.

Hello World Program to Demonstrate the Differences:-

Hello World in Java code

The prototypical Hello World example in Java code looks something like this:-

public class HelloWorld {
       public static void main(String[] args) {
          System.out.println("Hello World!");
      }
}
Compile and run the Java example

c:>javac HelloWorld.java
And finally, I run the resulting compiled class:

c:>java HelloWorld

Hello World in Groovy code

Groovy permits a relaxed Java syntax — for instance, you don't need to define classes for simple actions like printing "Hello World!"
What's more, Groovy makes everyday coding activities easier; for instance, Groovy permits you to type println instead of System.out.println. Groovy is smart enough to know you mean System.out when you type println.

Consequently, writing the Hello World program in Groovy is as simple as this:
println "Hello World!"
Note that there is no class structure around the phrase. There isn't a method structure either! I also used println instead of System.out.println.
Run the Groovy example

Assuming the code is saved into a file called MyFirstExample.groovy, run this example by simply typing
c:>groovy MyFirstExample.groovy

That's all it takes to get the words "Hello World!" printed out on my console.

Shortcuts in action

You might notice that we dont have to compile the .groovy file. That's because Groovy belongs to the family of languages known as scripting languages. One of the defining qualities of scripting languages is that they can be interpreted at runtime. (In Java, bytecode is also interpreted and generated as a result of compiling source code. The difference is that scripting languages interpret source code directly.)

Groovy permits you to drop the compilation step entirely, though you could do it, if you wanted to, using the Groovy compiler, groovyc. Compiling Groovy code with groovyc yields normal Java bytecode that we can then run via the java command. As for running the code,

c:>groovy -e "println 'Hello World!'"

Conclusion: -

Being able to code quickly in Groovy also means receiving feedback sooner, not to mention the satisfaction of crossing tasks off of your to-do list. At a high level, if you can put code in front of stake-holders more quickly, you can give them more releases in a shorter time. In essence, Groovy lends itself more to Agile development than Java does.

No comments:

Post a Comment