Skip to main content

Posts

Showing posts from January, 2012

Method parameters in Groovy

Groovy allows you to create a methods with optional parameter, but the trailing parameter is only optional, see the below example.
class MathLog { def log(x,base=10) { Math.log(x)/Math.log(base) } }  Here the base is optional parameter.

The main method.
MathLog mathLog = new MathLog()
println mathLog.log(10)
println mathLog.log(10,2)

 The function call  mathLog.log(10) takes the base value as 10, that's defined in the method itself but the function call mathLog.log(10,2) takes the 2 as a base value.

The output is
1.0
3.3219280948873626

Note:In Groovy the return statement is optional, because by default it's return the last line output to calling method.

Java bean vs Groovy bean

Java Beans.
   Generally the java bean class have a lot of variables and accessor(setter and getter) methods. The developer needs to write a setter and getter method for those variable as well as the developer explicitly needs to tell that variables and accessor methods are public. See the below example.


//The Country.java bean
public class Country {
    public String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

//The java main class
public class JavaBeanDemo {
    public static void main(String[] args) {
        Country country = new Country();
        country.setName("India");
        System.out.print(country.getName());
    }
}
The output is.
India.

Groovy Bean.
   But in Groovy it's easy because, the Groovy automatically generate accessor methods for all the class variable and by default the methods and variables have public access. See the below example.

Groovy Bean.
class Countr…

Safe navigation operator in Groovy

The Safe navigation operator(?.) is used for eliminating "Null" check in Groovy. See the below example.
 In java the null value check is like this.

   String str = null;
   if(str !=null) {
      System.out.println(str.reverse());
   }

Same thing in Groovy.

class SafeNavigationOperatorDemo {
    public static void main(String[] args) {
        def str = null;
        println str?.reverse()
        str = "Cool stufs...."
        println str?.reverse()
    }
}

This will print the message like this.
null
....sfuts looC

Loops in Groovy

The Groovy loops are very powerful and easy to use compared to Java. Here I printed 0 to10 integer values using both Java and Groovy language.

The java code.
public class LoopsInJava {
    public static void main(String[] args) {
        for(int i=0;i<10;i++) {
            System.out.println(i);
        }
    }
}

The Groovy code.
class LoopsInGroovy {
    public static void main(String[] args) {
        10.times {
            println it
        }
    }
}
Just notify the real power of Groovy now.
If suppose you want to print some even numbers that are in between 2 to 20 then, you can able do like this.

2.step(20,2) {
    println it
}

It is equivalent to
for(int i=2;i<20;i+=2) {
  System.out.println(i);
}

The output is.
2,4,6,8,10,12,14,16,18

Hello world application in groovy

Hi everyone, I am going to play withGroovy language. The dynamic behaviour is one of the key feature of this language and it also object oriented language that runs on the JVM. It include the some of the major features that are available in some other languages like Ruby, Python, and Smalltalk. The Groovy automatically include the groovy.lang.*, groovy.util.*, java.lang.*, java.util.*, java.io.*, java.net.*, java.math.BigInteger and BigDecimal packages into our application.
 As usual I started with my "Hello world" aplication.

class HelloWorld {
    public static void main(String[] args) {
        println "Hello World"
    }
}