10 basic differences between Java and Groovy Programming

If you are working in Java, you might have heard about Scala, Groovy, and Closure. Out of these three Groovy seems to be gaining a place in Java projects more rapidly than others. Groovy is a Scripting language but runs on Java virtual machine. Every Java program can run on Groovy platform. As per official Groovy website, "Apache Groovy is a powerful, optionally typed and dynamic language, with static typing and static compilation capabilities, for the Java platform aimed at improving developer productivity thanks to a concise, familiar and easy to learn syntax". I think, they have highlighted the key capabilities of Groovy very well in that sentence. It basically further reduce the burden from Java developer with respect to coding. Now, you can put more focus on business logic and get your work done quickly without wasting time on writing more code.

In this article, I'll introduce with some of the most fundamental differences between Java and Groovy. For some of you these differences might be trivial but a Java programmer starting with Groovy, these will give him a good idea about what to expect in Groovy. The general feedback I have received from Java developers who also use Groovy is quite positive, especially for small task e.g. dynamically generating config files, template based programming, testing using Spock framework and so on.

This list is by no means complete, in fact, it's the most basic list of differences between Groovy and Java you will ever find. If you want to contribute or highlight some other differences, please, feel free to leave comments, those will be very useful to a Java developer learning Groovy.




Groovy vs Java

Even though Groovy generate bytecodes similar to Java and runs on JVM, can use existing Java library, the programming style and capabilities are very different than Java programming language. Once you start writing code in Groovy, you will notice that how easy it is to do something in Groovy than Java. Anyway, here is my list of 10 basic differences between Groovy and Java programming language.


1) In Java by default access modifier is package i.e. if you don't specify access modifier for fields, methods or class it becomes package-private, on Groovy things are by default public. It means a method without any access modifier is public and accessible outside of class and package boundaries.


2) In Java, you need to provide getters and setters method for fields, especially if you are following Java Beans naming convention and use tools and libraries, which uses Reflection to dynamically access and mutate bean properties. In Groovy, getters and setters are automatically generated for class members.


3) Groovy is a superset of Java, which means a Java program will run fine in Groovy environment, but vice-versa may or may not run, depending upon whether it’s written in groovy or Java.

Btw, here is a nice image which perfectly highlights the productivity difference between Java and Groovy:

Difference between Groovy and Java



4) In order to make things concise, Groovy has provided several utility methods e.g. to print something on console, instead of using System.out.println, you can simply use println(). See Groovy in Action to learn more about optional things in Groovy which are mandatory in Java.


5) Another key difference between Groovy and Java is that groovy supports variable substitution using double quotation marks with strings i.e. you can print dynamic content without using string concatenation e.g.

println("Good morning $name !");

will include the value of name variable, which can be either local or member of the class. This is quite useful for writing scripts where you can use environment variables.


6) There is a difference between Java and Groovy on handling dot (.) operator. Java uses the dot operator to access properties and groovy also support dot operator but unlike Java call actually go through getters and setters, which is automatically generated in groovy. For example

   Greetings greet = new Greetings();
 greet.message = 'Good Morning';

will call mutator setMessage(String message) from the Greetings class.


7) One more key difference between Java and groovy is specifying type information. Typing information is mandatory in Java but optional in Groovy. In Groovy language instead of specifying a type, you can just use the keyword def as shown below. Use of def is mandatory for methods, but it is optional for method arguments. You can also use def with class and method variables to avoid specifying exact type. In this example, we have used def in place of the type for method return type, specifying type for variable and we have not specified any type for method argument as well, this program will not compile in Java but runs fine in Groovy.

class Greetings {
    def greet(name) {
        println("Good morning $name!");
    }

    static def main(args) {
        Greetings morning = new Greetings();
        def name = 'James';
        morning.greet(name);
    }
}

See Programming Groovy 2: Dynamic Productivity for the Java Developer for many such tips, which can drastically improve your productivity.

differences between Java and Groovy Programmming


8) Next difference between Java and Groovy is related to semicolons. In Java, every statement ends with a semicolon but in Groovy it's not required. Use of semicolons is totally optional in Groovy though I prefer to use semicolon because of programming in Java for so long, I used to treat semicolon as the end of the statement. If you are in a hurry and want to save few keystrokes, you are free to avoid semicolons.


9) In Java, you need the main method to make a class executable, in Groovy you don't need that. Since Groovy is a Scripting language, there's automatically a wrapping class called Script for every program. This means you can get rid of your own wrapping class, as well as the main method, like so:

def greet(name) {
        println("Good morning $name!");
}
def name = 'Steve'
greet(name)

This code will be wrapped by Script class so that it can be executed inside Java virtual machine.


10) There is some syntax difference between Java and Groovy as well, for example In order to create and initialize String array in Java, you can curly braces e.g. {"ABC"} but in Groovy, you need to use square bracket e.g. ["ABC"]. Here is how to create static array in Java and Groovy:

In Java:
String[] currencies = {"GBP", "USD", "JPY", "EUR", "INR} ;

In Groovy
String[] currencies = ["GBP", "USD", "JPY", "EUR", "INR] ;

Note the difference, in Java, array elements are enclosed with braces e.g. "{ }" but in Groovy we have used brackets "[ ]". Also worth nothing that under the hood, this code is no longer creating an array; rather, Groovy is (invisibly) creating an ArrayList. This gives us a number of new options e.g. names.sort(), very useful.


That's all about basic differences between Java and Groovy. The best part is that both can live together in the same project. I have seen projects where Groovy is used to automate build system, providing dynamic configuration and testing. The popular, Spock testing framework is also built on Groovy and it's slowly getting adopted by Java developers too. If you are interested in learning Groovy I suggest reading Groovy in Action, the most comprehensive book on Groovy for Java developers.

10 basic differences between Java and Groovy



Other Java articles you may like
  • Differences and similarities between Scala and Java (see here)
  • 5 Free Scala Programming books for Java developers (see here)
  • Java vs Python - which language is better to start with? (read more)
  • 10 Articles Every Programmer should read (see here)
  • 10 tips to become a better software developer (see here)
  • Difference between Java and JavaScript (see here)
  • Difference between an average and good programmer (read more)
  • 5 books to improve your coding skill (see here)


References
Groovy - a multi-faceted language for the Java platform



1 comment :

Arjun Sridhar UR said...

I donj't think we can use groovy to do complete project

Post a Comment