It is hard to believe I have been working on Java code for over ten years. I remember getting started in Java working on JSPs or Java Server Pages and Servlets which included putting scriptlets in my JSPs. Later on, I learned how that was frowned upon by true Java programmers. As we develop our Java skills we learn from senior team members as well as from books and blogs we read. Today, we are going to go over some ‘Java gotchas’ and how to avoid them.
Groovy is a lightweight, object oriented, dynamic language designed to supplement Java development. Running on the Java Virtual Machine (JVM), it can be used as a scripting language, or compiled down to bytecode. An open source language under the Apache 2.0 license, Groovy gives you the power of advanced language features like dynamic methods, closures and Meta Object Programming (MOP). Most importantly, Groovy is designed as a companion to, not a replacement for, Java.
What is a dynamic language?
Dynamic languages can extend an application at runtime to change behaviors, types and objects. With this functionality, it is possible to execute statements that are created at runtime. A dynamic language can accomplish common programming behaviors that statically typed languages must do at compile time.
Why did Groovy come about?
James Stachan invented Groovy over a decade ago. While he and his wife were traveling, waiting for a late airplane, James began reading about some of the features of Python. Hoping to bring functionality such as dynamic behaviors and common datatypes to the Java platform that he worked in everyday, James began to develop Groovy, taking cues from Ruby and Smalltalk, as well.
What makes Groovy different?
Groovy gives you the option to use semicolons, but if you want to leave them off, that’s okay too. Additionally, the return statement is optional. In Java you use .equals() to compare two objects and in Groovy you can use == to compare two objects. With Groovy you do not need to specify a type for a primitive. Instead, you can simply use def:
def blah = “I am a String!”
def pi = 3.14
Of course, you can still do it this way:
String blah = “I am a String!”
float pi = 3.14
Who should use Groovy?
Since Groovy runs on the JVM and interoperates well with Java, Java developers would find Groovy to be a good option. Also, Java code can run alongside Groovy code so the two can coexist nicely in projects. Java developers new to the Groovy language can always fallback to coding in Java and then come back later to refactor the Java code to Groovy once they are more comfortable with the tool.
What does it look like?
The Groovy syntax should look familiar to Java developers, with some subtle differences. Here is a comparison to create a new date. In Java it would take two lines:
Date today = new Date();
In Groovy we can do this with just one line of code:
today = new Date()
Groovy doesn’t need to import java.util package to use the Date type. Groovy imports java.util, java.io, java.net and many others by default.
Who is using Groovy?
Major corporations are using Groovy; they include:
- Best Buy
- Wells Fargo
- Cisco Systems
If you’re looking to expand your skill set while staying in familiar territory, give Groovy a try!