Difference Between Groovy and Java

Groovy is an optionally typed and dynamic programming language for developing the application on the Java platform. The syntax of Groovy is similar to Java. Groovy is a very powerful, strong type, dynamic and static programming language that extends JDK. By extending JDK, it can accept Java code. Groovy is not only used as a programming language but also as a scripting language. Java programs can run in the Groovy environment because groovy is a superset of Java. Programs written in other programming languages except Java may or may not be possible to run in the Groovy environment.

Groovy vs Java

Groovy is not only based on Java, but also it combines the power of other languages such as Python, Ruby, and Smalltalk. With these powers, Groovy provides more opportunities than Java.

Let's understand the differences between Groovy and Java:

S.No.FactorGroovyJava
1.Default importsThe java.io.*, java.lang.*, java.math.BigDecimal, java.net.*, groovy.lang.*, groovy.util.*, java.util.* and java.math.BigInteger are some general purpose classes and packages which are imported by default.The Java.lang.* is the only package that is imported by default in Java.
2.KeywordsIn the comparison of Java, Groovy has three extra keywords, i.e., as, defin, and trait.In Java, we cannot use this as a keyword.
3.Access modifierThe public is the default access modifier in Groovy.Package-private is a default access modifier in Java.
4.Getter and setterGroovy generates getter and setter automatically for the class members.Java doesn't provide getter and setter automatically for the class members. So, we need to define getter and setter methods for class members.
5.Dot operatorGroovy also supports dot operator but unlike Java, calls actually go through getters and setters, which are automatically generated in Groovy.The dot operator is used to access data members of a class.
6.SemicolonsUnlike Java, the semicolon is not required to use. It is only used to write more than one statement in a single line.In Java, the semicolon is required to use for the statements.
7.For loopIn comparison to Java, the declaration of for loop is much easier. We declare for loop in the following way: for(j in 0..4){ print j }
0.upto(3){
print "$it"
}
4.times{
print "$it"
}
In Java, we declare for loop in the following way:
for(int I = 0; I <= 5; i++){
System.out.println(i);
}
8.Safe Navigation OperatorIn order to avoid the null pointer exception, we don't need to perform an operation to check the null object.In Java, we need to perform operations to check whether the object is null or not to avoid null pointer exceptions.
9.Use of main() methodIn groovy, there is no need to define the main() method because it is also a scripting language and there is always have Script class(Wrapping class) for each program.In Java, we need to define the main() method to execute the class.
10.Boolean evaluationIn Groovy, the expression is automatically evaluated as Boolean.In Java, the expression doesn't evaluate automatically as Boolean.
11.Array declarationIn groovy, we use curly brackets("{}") for declaring an array.
String[] test1 = ["A", "B", "C"]
In Java, we use square brackets("{}") for declaring an array.
String[] test1 = ["A", "B", "C"]
12.Boxing and UnboxingThere is no concept of autoboxing and unboxing because here, everything is the object.In order to perform boxing and unboxing, Java has primitive data types and the concept of the wrapper class.

So, Groovy is a superset of Java Java, and we can run Java programs in a Groovy environment. Groovy and Java are both similar, and both have some small differences, which we defined above.