JEP 445 : Java vs Groovy for Education
For the longest time, I have said that Groovy is Java’s gateway drug… in the sense that it is easy to learn and pickup and it open the doors to the world of Java for those that find it overwhelming and verbose.
But the Java community has HATED Groovy for:
- its dynamic variables (Java10 added these in with ‘var’)
- scripting (Java is TERRIBLE for scripting)
- CLI (see scripting)
But these issues the community has are the same things that make numerous languages easy to pick up and learn: Python, Javascript, Ruby, Swift, Perl, PHP. Combined, there are more than 2x the number of people employed in scripting languages than in Java.
Recently however, seeing the encroachment of scripting languages, Java is trying to make their language more educational friendly to get more people onboard by making it more scriptable and CLI friendly… so I thought I would do a friendly comparison between the two JVM languages: Groovy which was made for scripting/CLI and Java (which wasn’t).
Scripting : Java vs Groovy
The JEP445 specification suggests an ‘anonymous’ main method for scripting in Java. Here is a ‘Hello World’ in Java and Groovy:
JAVA:
public class HelloWorld {
void main() {
System.out.println("Hello, World!");
}
}
Groovy:
println("Hello, World")
Which do you think is easier to learn? Java developers always say the first one (and have their excuses).
CLI : Java vs Groovy
Java has no real solution for CLI and has numerous libraries to try to mimic writing shell scripts. The most popular is piccoli.
EDIT: The story has been changed to show JEP 330 which allows Java classes to be called like bash scripts
#!/path/to/java --source version
import picocli.CommandLine;
import static picocli.CommandLine.Command;
@Command(name = "hello",description = "Says hello")
public class HelloWorldCommand implements Runnable {
public static void main(String[] args) {
CommandLine.run(new HelloWorldCommand(), args);
}
@Override
public void run() {
System.out.println("Hello World!");
}
}
Groovy is simple. Take the Groovy ‘hello world’ and just add your she-bang for your groovy bin directory:
#!/usr/bin/env groovy
println("Hello, World")
Boom! done.
Again, guess which Java users say is easier to learn? Don’t ask.
Knowledge Transferability
Because Groovys syntax is similar to other langs, people who have learned other languages can transfer those skills in order to pick up JAVA.
For example, below is the initialization of a list in Groovy. And we follow it with the initialization of a list in several other languages…
GROOVY (initializing a list)
def newList = [1,2,3,4,5,6,7,8,9,10]
PYTHON:
newList = [1,2,3,4,5,6,7,8,9,10]
RUBY:
newList = [1,2,3,4,5,6,7,8,9,10]
JSON:
[1,2,3,4,5,6,7,8,9,10]
This makes the learning curve even easier if you have any experience in software development at all and the skills far more transferable… which is what management teams look for.
Kotlin: Honorable Mention
The Java community is happy to ‘hug’ Kotlin and shit on Groovy… mainly because Kotlin is STATICALLY TYPED FIRST whereas Groovy is DYNAMICALLY TYPED FIRST. But Kotlin is a close second to Groovy for educational purposes. And heres why:
- Syntax — Kotlins syntax is closer to Java, even when it is TRYING to be easier.
KOTLIN (initializing a list)
val newList = listOf<Integer>(1,2,3,4,5,6,7,8,9,10)
GROOVY (initializing a list)
def newList = [1,2,3,4,5,6,7,8,9,10]
Still simpler than Java.
- Learning Curve — While Kotlin is still easier than Java, it is nowhere near as easy to learn as Groovy for beginners. Some may disagree but there is YEARS of evidence for this online. But just to show the difference in the learning curve again, lets show merging two lists:
KOTLIN:
val list1 = listOf<Integer>(1,2,3,4,5)
val list2 = listOf<Integer>(6,7,8,9,10)
var finalList: List<Integer>? = merge(list1, list2)
GROOVY:
def list1 = [1,2,3,4,5]
def list2 = [6,7,8,9,10]
def finalList = list1.addAll(list2)
Java is seen as verbose and Kotlin is just SLIGHTLY less verbose.
- Compiling — Groovy is designed to be dynamically typed; static typing is done through an override. Kotlin is designed to be statically typed; it’s dynamic typing is actually a static object called ‘dynamic’ (it converts at run-time).
- Code Style — Groovy allows for functional / imperative style coding while Kotlin is a bit more limited through it’s IDE’s.
In Closing…
If you are an educator wanting to introduce students to Java, I think you can see Groovy is your path to the JVM without overwhelming them. But if you agree with the Java community and that they should be thrown into the deepend and sink or swim…well then I wish your students the best learning Python. :)