Introduction to Groovy
Apache Groovy is a dynamic and agile language which is similar to Python, Ruby, Smalltalk etc. It's a powerful, optionally typed and dynamic programming language which is based on Java.
Key Features
- It's not a replacement for java but it's an enhancer to Java with extra features like DSL support, dynamic typing, closures etc.
- Accepts Java code as it extends JDK and can integrate smoothly with any java program.
- Greater flexibility
- Concise and much simpler compared to Java
- Can be used as both programming language and scripting language.
Installation
1. On Windows
- Download the software from Groovy Downloads
- Double click on the exe file and the setup will guide you through the installation process and any settings if required.
- run the command
groovysh
in the command prompt to check if the installation is done correctly.
2. On Mac OSX, Linux, Cygwin, Solaris or FreeBSD
Installation of Groovy is very easy with the help of SDKMAN( Software Development Kit Manager).
Open terminal and execute the below command
curl -s get.sdkman.io | bash
Open another new terminal and type the below command:
$ source "$HOME/.sdkman/bin/sdkman-init.sh"
To install the latest stable Groovy:
$ sdk install groovy
Once installation is complete, test using the below command.
$ groovy -version
3. Using OneCompiler
- You don't need to install any software or compiler.
- Just goto OneCompiler and choose the programming language as
Groovy
and enjoy programming without any installation.
Sample Program
println "Hello World!"
Try yourself here
The above program when written in Java:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
As you can see Groovy is much simpler and concise. You don't need to worry on curly braces or paranthesis and no need to write your program in main(). Even the print statement is much concise. This is because Groovy internally converts the script into the program similar to java program with class and main().