Groovy is powerful, optionally typed and dynamic language to develop an application on Java Platform where its syntax is Java-like. Its typing discipline is strong, static, and dynamic. The best things about Groovy are that since it extends JDK, it accepts Java code. Groovy can be used as both programming and scripting Language. Groovy is a superset of Java which means Java program will run in Groovy environment but vice-versa may or may not be possible. Whereas Java is strongly and statically typed programming language.
1. Default Imports:
- In Groovy some general purpose packages and classes are imported by default:
- java.io.*
- java.lang.*
- java.math.BigDecimal
- java.math.BigInteger
- java.net.*
- java.util.*
- groovy.lang.*
- groovy.util.*
- Where in Java only java.lang.* package imported by default.
2. Extra Keywords:
- as, define, trait these are extra keywords in Groovy.
- You can not use this keywords as variable name.
3. Default access modifier:
- In Java default access modifier is package i.e. if you don’t specify access modifier for fields,methods or class it becomes package-private.
- In Groovy it’s by default public.
4. Getters and Setters:
- 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.
5. The dot separator:
- In Java, we use the dot operator (.) to access the property of the class
- Groovy also supports dot operator but unlike Java calls actually go through getters and setters, which is automatically generated in Groovy
6. In Groovy semicolons(;) are optional, use them only if you like or if you want to write more than one statement in one line.
7. For loop:
- As in most of the tasks we use a for loop, in Groovy declaring for loop is much easier, we can declare a for a loop as
for(j in 0..4){ print j } 0.upto(3) { print "$it" } 4.times{ print "$it" }
- wherein Java
for(int i=0;i<=5;i++){ System.out.println(i); }
8. Safe Navigation Operator:
- In Java we have to perform some operation to check the null object, to avoid null pointer exception.
String str = null; if(str != null){ System.out.println(str.reverse()); }
- But in Groovy we can do as below directly
println str.reverse()
9. The main() method:
- 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.
- 10. Boolean Evaluation:
Groovy automatically evaluates the expression as boolean.
def str = “test” if(str){ println str }
11. Array Declaration:
- If you want to create String Array in Java then you have to use curly braces “{}”.
In Java:
String[] testArray = {"A", "B", "C"};
- Whereas in Groovy we can use square brackets. “[]”.
In Groovy:
String[] testArray = ["A", "B", "C"]
Summarise difference between Java and Groovy
Java | Groovy |
---|---|
It is developed on JDK and is run on JVM | It is compiled to JVM Byte code and It is compatible with Java platform |
It is used as programming and object oriented Language | It is used as both programming and scripting Language |
In Java default access modifier package | In Groovy default access modifier public |
In Java, you need to provide getters and setters method for fields, especially if you are following Java Beans naming convention | In Groovy, getters and setters are automatically generated for class members |
In Java semicolons are compulsory | In Groovy semicolons are optional |
In Java only Java.lang.* package is imported by default | In Groovy commonly used packages are imported by default |
Java has primitive data types and wrapper classes to perform boxing and unboxing implicitly or explicitly | In Groovy everything is object and uses only object hence no concept of autoboxing or unboxing |
Java has a requirement of the main method inside a class to run the program | Groovy does not require any main method or entry point of a method to run the class or any program |