Difference between Java and Groovy


Java as well as Groovy code run on the Java Runtime environment. Groovy code is compiled to the Java byte-code. Groovy was primarily written with the objective to easy the code writing compared to Java but still run on the JRE.

  • Groovy language along with the Grails Framework revolutionized simple and easy application building on the JRE.
  • Groovy and Grails were created inspired by Ruby On Rails.
  • Groovy supports object oriented programming, meta-programming and functional programming.
  • Groovy supports many interesting concepts such as traits which support multiple inheritance without the diamond problem.
  • Curry is another interesting feature supported by Groovy to create a modified version of closure (used in other languages) by providing default values to some of the closure parameters

Java Groovy
Groovy code does not compile within a Java project All valid Java code works in Groovy as well
Only java.lang package is imported by default Lot of Standard Java packages such as java.lang, java.io, java.net, java.util are imported into groovy code by default
Method to be executed is determined at compile time Method to be executed is determined at runtime
Braces { and } are used for Array initialization Square brackets [ and ] are used for Array initialization
Not specifiying a scope will result in default scope being considered Not specifying a scope will result in private scope being considered
Java 8 supports lambdas and method references Groovy supports closures
Spring boot provides convention-by-configuration for Java language Grails provides convention-by-configuration for Groovy language
Spring ORM provides support for Hibernate for Java language Grails GORM provides support for Hibernate for Groovy language
Java Strings are created with double quotes Groovy Strings are created with single or double quotes
Java Strings do not support templating by default Groovy GStrings provide templating ability by default
Java not suitable for scripting Since Groovy has a lot of defaults like no need of class name, no need of imports for some of java packages, simple declarations of maps and arrays, it is suitable for scripting
Java does not autowrap primitive unless actually needed. Java prefers widening of primitive data type over autoboxing. For example, int is preferably converted to long over converting to Integer Groovy autowraps all primitives by default. int is converted to Integer by default
For Java objects, == checks only for identity and does not check for equality For Groovy objects,== checks for comparison if the object supports Comparable interface and checks for equality otherwise
In Java, we have to use Object class to refer to anything In Groovy, def keyword can be used
Java does not have easy support for meta-programming compared to Groovy language Groovy supports meta-programming with ease