Open In App

Difference between Groovy and Java

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: 



2. Extra Keywords: 

3. Default access modifier: 



4. Getters and Setters:

5. The dot separator:

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:

for(j in 0..4){ print j } 
0.upto(3) { print "$it" } 
4.times{ print "$it" }    
 for(int i=0;i<=5;i++){
  System.out.println(i);
 }

8. Safe Navigation Operator: 

String str = null;
if(str != null){
    System.out.println(str.reverse());
}
println  str.reverse()

9. The main() method:

def str = “test”
if(str){ println str }

11. Array Declaration:

String[] testArray = {"A", "B", "C"};
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

 

Article Tags :