I love the Apache Groovy programming language. I’ve loved it since I first ran into it in 2008. My initial positive reaction to Groovy was based on two factors: it was very low ceremony, much like Python and it grew out of Java. For me, that was a “best of both worlds” thing.
Groovy lets you access classes from java.lang.String
to java.util.HashMap
. You can also access packages like java.sql
and java.collections
. In many cases, they’re enhanced or streamlined to be readily accessible through direct syntactic support.
For example, in Java you would write:
import java.util.LinkedHashMap;
…
var addressMap = new LinkedHashMap<String,String>();
…
var employeeName = “Smith, Bob”;
addressMap.put(employeeName, “1824 Anderson Court, San Francisco, CA”);
System.out.println(“Address of “ + employeeName + “ = “ +
addressMap.get(employeeName));
In Groovy you would write:
def addressMap = [:]
…
def employeeName = “Smith, Bob”
addressMap[employeeName] = “1824 Anderson Court, San Francisco, CA”
println “Address of $employeeName = ${addressMap[employeeName]}”
Take a moment to go through the program:
- Groovy auto-imports most of the Java standard libraries so you don’t have to.
- Groovy lets you define dynamically typed variables using the
def
keyword (would be like declaring a Java variable to bejava.lang.Object
) and then access type-specific attributes and behavior at runtime. - Groovy has syntactic support for maps using
[]
to get and set the value based on the variable within the square brackets. You can still useget()
andset()
if you wish. - Groovy has a string interpolation facility called
GString
which you can see at work in theprintln
method call above. - Groovy usually doesn’t need semicolons or method call parentheses.
- Where did
System.out
go?
For me, Groovy was — and still is — a big win for scripting. The code is shorter than Java, which makes it easier to read and understand because there’s less distraction. When I’m scripting and I don’t feel like defining all sorts of classes to hold my data, I can just shove it into maps or lists. Of course, with great power comes great responsibility.
My intent in this series is to give one compact Groovy lesson per day, to encourage you to try it out for yourself. If you have a strong background in Java and find yourself using Python to deal with scripting tasks, I’m betting I can convince you to give Groovy a try.
If you’re intrigued with the idea of trying Groovy, you need to first install Groovy and Java on your Linux laptop or desktop.
Install Java and Groovy
Groovy is based on Java and requires a Java installation. A recent version of Java and Groovy might already be in your Linux distribution’s repositories. Groovy can be also installed following the instructions on the official Groovy website. A nice alternative for Linux users is SDKMan, which can be used to get multiple versions of Java, Groovy and many other related tools.
This series kicks off using SDK’s releases of:
- Java: version 11.0.12-open of OpenJDK 11
- Groovy: version 3.0.8.
If I get to the point where I want to use something requiring a newer Groovy, I’ll be sure to mention it.
This post is part of a series on Apache Groovy, stay tuned for more.
Photo by Brittani Burns on Unsplash
3 responses to “Meet Apache Groovy: The programming language that combines the Best of Python and Java”
[…] need to know as we dive deeper into Apache Groovy. (If you haven’t installed Groovy yet, please read the intro to this […]
[…] not just in a limited situation such as Java Records (If you haven’t installed Groovy yet, please read the intro to this […]
[…] Groovy is a powerful, concise and easy-to-learn language; I’ve loved it since 2008. My mission with this series is to show what it can do for you. (If you haven’t installed Groovy yet, please read the intro.) […]