Engineering Full Stack Apps with Java and JavaScript
Groovy is designed as a companion to Java and not as a replacement language for Java. Groovy interoperates with other Java code and libraries smoothly. Everything you can do with Groovy can be done using Java, but Groovy help us reduce the amount of code written to a great extend.
You can call Java from Groovy, as with most other JVM based languages. Groovy dynamically compiles to Java Virtual Machine (JVM) bytecode. Every Groovy type is a subtype of java.lang.Object. So once a Groovy class is compiled to a .class file, it can be used from within Java code just like any other Java class.
A Groovy class can reference a Java class and vice versa. Groovy compiler groovyc even allows circular dependencies. Interfaces defined in Groovy and Java can be implemented from the other. We can have inheritance hierachy with Groovy and Java classes in any order. Groovy Development Kit (GDK) adds libraries and even adds functionality to existing Java libraries.
When used together, we may use Java for code that needs to have better runtime performance and Groovy for code that needs to be more flexible and readable. Groovy may be used for Automation usecases such as build automation, continuous integration, reporting, shipment, installation etc. Groovy can be also used to create quick prototypes as part of design discussions.
In Java we can create a Date object reference as follows:
import java.lang.*;
Date date = new Date();
In Groovy we can do as:
date = new Date();
Groovy is mostly used in a dynamic way, but Groovy also has static-typing and static compilation capabilities. You can decide whether to use static typing or whether to use dynamic programming.