Sunday, April 16, 2023

Groovy - 10 things Java Developers should know

Last year, I got a chance to work with Groovy with one of my client's project. The project which uses groovy a lot for unit testing and configuration build along with some templating engine. In order to understand those big and small Groovy scripts, I started learning Groovy by reading Java Make Groovy. I have to accept that learning a new language is not an easy job, it takes both time and effort to be comfortable with a language even if the language seems relatively easy. Groovy has been designed keeping Java developer in mind and it aims to complement Java by addressing issues which both developers e.g. its verbosity and making things easier for them. 

Unlike other JVM languages like Kotlin and Scala, it doesn't compete with Java and that's why I feel Java developer should learn Groovy. Ultimately, it is another tool in your arsenal to do things which was not easy with Java. 

As I told, learning a new language is not easy. It's not enough to read book once, you have to read it a couple of times and practice a lot. You have to keep refer and practice until you become familiar with the syntax and API, but after each reaching certain things will stick on your mind. Those are the things are which are essential and must know stuff for every programmer. 

10 Groovy Concepts Java Developer Should Learn

Today, I am going to share some of the groovy concepts which stick with me after my first reading of Make Java Groovy, and Groovy 2 in Action. These are some of the essential concepts on Groovy and help you on both reading Groovy code and changing them. They are not enough to write groovy code from scratch but atleast give you some idea from where to start. 

1) Groovy truth

One of the thing which I liked about groovy is its flexibility, which gives it power. In Java, only boolean can be true and false but in Groovy a non null object, non empty collection, non empty string and non zero number can also be true. Which means you can use any of these data type inside if condition and it will work as shown below:

assert true // boolean true
assert [101] // non empty collection true
assert [one:1] // non empty map is true
assert 1 // non zero number is also true

Apart from that even a Matcher which has match and Iterator which has more element can resolve to true. I suggest you to read Groovy in Action second edition to learn more about Groovy truth. 


2) Native syntax for collections and array

Another thing I liked about Groovy is the native syntax for collections e.g. you can use square braket to declare both array and list and [:] to create a map. This is super easy, you don't need to use antipattern like this to create and initialize a list or map in the same line of declaration. Here is an example of list and map in Groovy

def list = [one, two, three, four, five]
def map = [one:1, two:2, three:3, four:4]


3) Operator overloading

One of the drawback of Java was that it doesn't support operator overloading, which sometime because nuisance, especially in big financial calculation, where you have to use BigDecimal but you cannot add, substract or multiply them using +, - or * operator. Groovy solves this problem by providing operator overloading and one of the interesting example of this is the left shift operator (<<) which can be used to append elements in list, string etc. 

4) semicolon is optional

Yes, unlike Java semicolon is optional in Groovy. The groovy compiler is smart enough to find when a statement ends.

Groovy - 10 things Java Developers should know


5) Every Java code can run in Groovy but not vice versa

Yes, you can just copy paste Java code in your groovy file and it will work but you cannot do same. A groovy compiler, which is groovyc understand Java but obviously java compiler javac doesn't understand Groovy

6) Sometime parenthesis is also optional

Yes, if there is just one argument then parenthesis is also optional. I am still learning exact rule when can you omit parenthesis but this is one of the simplest case. For example, you can just write println "Whatever" as oppose to System.out.println("Whatever". I suggest to read Make Java Groovy to larn more about it. 

7) Groovy String

Groovy has increased the power of String given it usefulness. In Groovy, you will find mainly three kinds of String literal e.g. single quote, double quote and triple quote string. The difference between single quote and double quote string is that later allows for variable substitution which wasn't available in Java. 

The triple quote can be triple single quote(''') or triple double quote (""") which is used for multiple string, removing a lot of string concatenation involving "\n" character in Java.

The single quote string - '' is character in Java but they are also string in Groovy e.g. 'hello Java'
double quote string - allows variable substitution, example "hello $name"
triple quote string - allows multiline string e.g. 

''' ==============
version: 8.2
==============='''

You can read more about string literals in groovy in Groovy in Action book, what I know about string in Groovy, have learned from there only. 

8) New methods from GDK

GDK stands for Groovy Development kit which is counterpart of JDK, the Java Development Kit. It provides additional classes and methods to enhance existing classes in JDK e.g. you have GString class which allows variable substitution functionality. Similarly, Collection classes also got lots of useful methods. 

9) Closure

In Groovy, you can pass code to functions, which wasn't possible in Java until JDK 8. This means you can do functional programming using Groovy even if youa re not running in JDK8. 

10) Optional Typing

Groovy is an optionally typed language, which means its not mandatory to spcify the data type while declaring a variable. The general rule is, if you know the type then you should specify it otherwise you can use the def keyword e.g. 

String name = "Java"; //ok because you know name can be string
def value = "Java"; // also ok, because you don't know what type will value variable be. 

Since Java is a strongly typed language, this optional typing will take some time to used to. 


That's all about  interesting and essential things about Groovy, every Java developer should know about it. I strongly feel that Java developer should learn Groovy to compliment their Java skill. Groovy is easier to learn because it is exclusively written for Java developer and it complements Java programming language and JDK instead of competing with it. It gives you more power to do lot more in less code.

No comments :

Post a Comment