What is Groovy - Differenve between Groovy and Java

Groovy is known as Java scripting language and a lot of groovy users are taking advantage of its flexible nature. Groovy is better known as a new age advanced substitute of Java, or we can say it a better Java. Groovy takes advantage of static Java compiler and is purely a JVM based language, it generates bytecodes like Java and provides Java stability and trust in a better and flexible way.

Groovy is a JVM based dynamic language that runs on the Java Compiler. Groovy contains Java like syntax and design pattern most of the Java code is also valid in groovy but not all, rather groovy is much more dynamic and a new version of Java in a better and productive manner.

Although Java is a very good and widely used programming language but implementing some tasks like string manipulation and file handling is a a great pain in Java. Using Groovy one can implement and code difficult stuffs in a very dynamic and easy way. Groovy files have an extention of '.groovy' and unlike Java we do not need to put a semicolon after each programming statement. If you knows Java than its very easy to learn and implement groovy in your programming stuff. There are a number of similarities and some differences in between Java and groovy, some of them are listed below :

Groovy is designed as much as familiar with Java and tried to make general purpose tasks easy.

Difference between Groovy and Java

1 - In Java we have 'java.lang' package imported by default, but now in groovy we have some others general purpose packages and classes that are imported by default some of them are :


That means using groovy we do not need to import these packages explicitly, this feature gives groovy developers a quick development start.

2 - In java we uses '==' to compare primitive types and '.equals()' to compare two objects, but in groovy we can use '==' to compare both     primitive and object types. Although in groovy '.equals()' also works for comparing object.

3 - In groovy semicolons are optional, use them only if you like or if you want to write more than one statement in one line.

4 - In groovy 'in' is a keyword and we can not use it as a variable name.

5 - When declaring an array we can not write

    int arr[] = {1,2,3,4...}

    rather we need to right it as

    int arr[] = [1,2,3,4...]

6 - As in most of the tasks we use a for loop, in groovy declaring for loop is much easier, we can declare a for loop as

    for (i in 0..size-1) {}
    for (i in 0..<size) {}
    size.times {}

    Although for(int i;i<-size;i++){} is also valid in groovy.

7 - In java it is necessary to return a value if the method is not a void one, but in groovy return statement is fully optional. Write a return statement only if you want to return something from a method.

    int addNumbers(){
        sum = num1+num2

is perfectly valid in groovy and does not gives a syntax error for return statement.

8 - In groovy all methods and classes are public by default, you dont need to add a public keyword to make them public.

9 - In groovy there is no difference between a 'checked' or a 'unchecked' exception, so if you are writing 'throws' keyword in your code, this is not going to be compiled by groovy compiler.

10 - In groovy you do not need to specify a primitive type like int, float... or a class type to create an object, All you need is to put a keyword 'def' and groovy compiler is smart enough to detect type of value or reference you are assigning to a variable.

def a = 10
def f = 3.4
def str = "Hello World"

these statements are completely valid in groovy, although we can use java like declaration as well

int a =10
float f = 3.4

String str = "Hello World" ,these are also acceptable.

11 - While declaring an parameterised method in groovy, we do not need to specify types for parameters.

    def getValues(number1, string1,....)

this thing is perfectly right in groovy, although you can do this as

    def getValues(def number1,def string1,....)

or you can do this in jva style as well like

    def getValues(int number1,String string1,....)

12 - In groovy, while calling a parameterised method its not necessary to pass parameters as:

If we have a method like

    def getValues(int number1,String string1,....)

Than it can be called as obj.getValues() , you will not get any error for not providing parameters values. But if parameters are passed than they must be matching to required type.

13 - In groovy inner classes are not supported, groovy provides closure implementation that covers need of inner classes.

There is much more to add here, but the point mentioned above will give you a quick and better understanding about, what is groovy ?, difference between java and groovy and other things. Let me finish this discussion by saying if you are a java guy or you have coded in java already than its very easy to start with groovy. At least give a chance to groovy and feel the difference.