Difference between java and groovy

This blog shows you some differences between java and groovy. Basically if your background is java language, you are going to love it, because Groovy is something like a super version of java programming language. Which supports you are expecting from coming versions of java like in java 7 or java 8 are already there inside Groovy.

There are several differences, some important differences that can be noted for java developers are listed below.

Default imports

The java.lang package is the default import in Java programming language, i.e you need to import the classes which are there inside java.lang package, groovy is ahead from this default import. Below is the list of default imports in groovy.

  • java.io.*
  • java.lang.*
  • java.math.BigDecimal
  • java.math.BigInteger
  • java.net.*
  • java.util.*
  • groovy.lang.*
  • groovy.util.*

So in Groovy if you are using any of the classes or classes inside package listed above, you no need to explicitly import in your Groovy class file, they are by default imported such as java.lang is by default imported for java language.

Object Equality check by ‘==’
In groovy the double equals operator ‘==’ means equals on all types either for primitive or derived. In Java there’s a wired part of the syntax where double equals operator ‘==’ means equality for primitive types and == means identity for objects.

As the Groovy is using auto boxing for the primitive defined, Groovy convert all the primitives defined to derived object, so there is not need to put the confusion for ‘==’ and equals() method. So for simplicity in Groovy ‘==’ means equals().

Groovy provide a different way to real need to checking identity of object and that is ‘is()’ method. Groovy says, You can use the method “is” like foo.is(bar). This does not work on null, but you can still use == here: foo==null.

Keyword ‘in’ in Groovy

There is a new keyword defined in Groovy but not there in java, the keyword is ‘in’, so you cant use ‘in’ as a variable name in Groovy.

Array are differently defined in Groovy

As you know you can defined the array in Java as:

int[] array = {1,2,3,4,5};

But the same you cant use in Groovy, In Groovy you need to define something like below code.

int[] array = [1,2,3,4,5]

You can notice the array can be defined inside the square brackets.

Semi columns are optional

The semi columns are mandatory in java, but they are optional in Groovy, Even there can be issue to making semi columns optional, yes there are some concerns but you need to take care while programming when to use semi columns if required. There are some situation where you need to use the semi columns to running your groovy application smoothly.

If you are not putting any semi columns for statement in groovy class, the groovy differ them from new line. So if you are going to put some statements in one line you need to put semi columns after each statement, other wise groovy understand the whole line as one statement and something will go wrong.

Return is optional

In java there has to be return statement if your method has the return type, The return statement is optional in Groovy, Even you can put it if you like to put. The groovy return the last statement of the method as return type.

Default is public

There are four access modifiers for class and method in java these are public, private, protected and default, If you do not put any access modifier for class or method, it is default by default.

But in Groovy the default modifier is public and there is no more ‘default’ modifier. Groovy understand the default modifier of java is redundant, so they deleted default modifier.

So there are only three access modifiers available in Groovy and those are public, protected and private. The protected and private access modifier for class and method has the same meaning as in java

No support for inner classes

There are no support for inner classes in Groovy. Groovy provides a powerful feature of Closure which cover the necessary need of inner classes.

No compile time error for wrong type

As you know the Groovy the dynamic language, the most of things are done on the fly. So you will not get any compile time error for using undefined members or passing arguments of the wrong type.

This compile time check is strongly done in java.

The above are some of differences between Java and Groovy, apart from these differences the Groovy added some new features which are currently not there in any version of java, some of these new features are expected in coming version of java like java 7 and java 8.

New features added in Groovy which are not there in Java

  • Closures are very powerful feature in Groovy
  • Native syntax for lists and maps
  • GroovyMarkup and GPath support
  • Native support for regular expressions
  • Polymorphic iteration and powerful switch statement
  • Dynamic and static typing is supported – so you can omit the type declarations on methods, fields and variables
  • You can embed expressions inside strings
  • Lots of new helper methods added to the JDK
  • Simpler syntax for writing beans for both properties and adding event listeners
  • Safe navigation using the ?. operator, this will check the null of object like object?.anyFiled or object?.anyMethod(), So you no need to put more ‘if’ block to check the null object.

We will take a close look of each of new feature in different blog. For more information on the difference between Java and Groovy you can refer the Groovy page here