Difference between Groovy and Java

08 Sep, 2021
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.BigDecimaljava.math.BigIntegerjava.net.*java.util.*groovy.lang.*groovy.util.*
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 
 
arorakashish0911
Difference Between
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Difference between var, let and const keywords in JavaScript
Difference Between Method Overloading and Method Overriding in Java
Differences between JDK, JRE and JVM
Stack vs Heap Memory Allocation
Difference between Clustered and Non-clustered index
Arrays in Java
Arrays.sort() in Java with examples
Split() String method in Java with examples
Reverse a string in Java
Object Oriented Programming (OOPs) Concept in Java