Blog

In this blog first I am looking into two languages Java and Groovy both are JDK based languages . Java is strongly and statically typed language whereas Groovy is dynamically and optionally typed language .

The best things about groovy is that Since it extends JDK , it accepts the java code . You’ll see that your code is doing the same things, but it’s a lot smaller.

With Groovy we have power of Java too .

Let us see see some features of Groovy and we compare it with Java .

Thanks

Abhimanyu

It follows Java semantics.

It is dynamic language.

It extends the JDK.

Loop

In java if we have to iterate using for Loop our code will be like this

for(int i=0;i<=4;i++){
System.out.println(i);
}

In above code there is declaration and initialization of variable , there is comparision with every iteration and increment operator

In Groovy , we could do same thing in following ways

for(j in 0..4){print j}
0.upto(3) {print "$it"}
4.times{print "$it"}

Now if we want to print 0,2,4,6,8 and 10 using for loop , what we will do in Java is

for(int i=0;i<=10;i=i+2){
System.out.println(i);
}

And in groovy

0.step(10,2){print "$it"}
2. System level process in Groovy and Java

System-level Process

In Java,we can use java.lang.Process to interact with a system-level process. let us see the Java code

Java forces us to handle checked exceptions.if we use Thread’s sleep( ) method. Java will force us to catch java.lang.InterruptedException.

try
{
Thread.sleep(5000);
}
catch(InterruptedException ex)
{
}

What if we want to suppress the exception . We will simply add an empty catch block . Having an empty catch block is worse than not handling an exception. Exception is propagated to the caller who either can do something about it or can pass it yet again to its caller.

Now a lot of empty catch blocks will make our code look dirtier .Groovy does not force programmer to handle exceptions that he/she don’t want to handle or that are inappropriate at his/her level. Any exception we don’t handle is automatically passed on to a higher level.

I used catch(ex) without any type in front of the variable ex so I can catch just about any exception thrown my way. but this doesn’t catch Errors or Throwables other than Exceptions. To catch all of them, we will use catch(Throwable t).

That’s all too familiar Java code, isn’t it? The output from the previous code is as follows:

Year: 2008
Miles: 0
Setting miles
Miles: 25

You have to write getter and setter functions for the variables in Java . How you will feel if getter and setter function will be create internally , isn’t it will be good . Code will be less and will look more code . Groovy will do it for you

If you want a property to be read-only,then declare it final. This is not defining a final field but a read-only property—you can change the property from within instance methods of the defining class, but not from outside. Groovy provides a getter in this case and no setter .

Groovy gives you the flexibility to initialize a JavaBean class. When constructing an object, simply give values for properties as comma-separated name-value pairs. This is a post construction operation if your class has a no-argument constructor. You can also design your methods so they can take named parameters. To take advantage of this feature, define the first parameter as a Map.

The instance of Chair took type, height, and width parameters as name-value pairs. In the call to the access( ) method, you set the first parameter, weight, to a single value, which is an Integer. You set the last parameter, fragile, to a single value as well. The rest of the parameters in the middle are name-value pairs for location.

The example did not quite follow the same ordering as defined in the method definition .

In Groovy If the number of parameters one send is more than the number of arguments the method expects and if the excess parameters are name-value pairs, then Groovy assumes the first argument of the method is a Map and groups all the name-value pairs together as values for the first parameter.

Why not to use def all the time?

Previous example can lead us to problem , such as when one pass three integer arguments. In this case, the arguments will be passed in order, no map is created from the arguments, and the result is not what he/she desire.

one can avoid confusion like this by explicitly naming the first parameter as a Map, as shown here: