🔥 🔥Practical Open Source is coming 🔥🔥 Propose an article about doing business with Open Source!

Meet Apache Groovy: The programming language that combines the Best of Python and Java

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 be java.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 use get() and set() if you wish.
  • Groovy has a string interpolation facility called GString which you can see at work in the println 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

Disclaimer: All published articles represent the views of the authors, they don’t represent the official positions of the Open Source Initiative, even if the authors are OSI staff members or board directors.

12 responses to “Meet Apache Groovy: The programming language that combines the Best of Python and Java”

  1. […] need to know as we dive deeper into Apache Groovy. (If you haven’t installed Groovy yet, please read the intro to this […]

  2. […] not just in a limited situation such as Java Records (If you haven’t installed Groovy yet, please read the intro to this […]

  3. […] 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.) […]

  4. […] operator, inherited from the C-language with Java. (If you haven’t installed Groovy yet, please read the intro to this […]

  5. […] and leverage Groovy’s features effectively. (If you haven’t installed Groovy yet, please read the intro to […]

  6. […] primarily created as instances of the class String. (If you haven’t installed Groovy yet, please read the intro to […]

  7. […] me who constantly wrestle data into submission. (If you haven’t installed Groovy yet, please read the intro to […]

  8. […] implementations, which are already pretty cool.  (If you haven’t installed Groovy yet, please read the intro to […]

  9. […] with data structures in Java-based projects. (If you haven’t installed Groovy yet, please read the intro to […]

  10. […] making you a more efficient Groovy developer. (If you haven’t installed Groovy yet, please read the intro to […]

  11. […] again, you see that the Groovy approach is to make the classes you already know — like File — more useful by adding a new behavior to […]

  12. […] that make file handling concise and efficient. (If you haven’t installed Groovy yet, please read the intro to […]

Author

Support us

OpenSource.net is supported by the Open Source Initiative, the non-profit organization that defines Open Source.

Trending