FREE Subscription to Dr. Dobb’s Digest: Same Great Content, New Digital Edition
Site Archive (Complete)
Java Blog: Groovy 1.0 Released
Java
SWAINE'S CAFE

Black. No Sugar. Extra Caffeine.

by Mike Swaine
ERIC BRUNO'S BLOG

Java: The Daily Grind.

by Eric Bruno
January 04, 2007

Groovy 1.0 Released

Groovy is a dynamic scripting language that works with Java, and can call Java libraries.

As described on the Groovy site, "Groovy is an agile dynamic language for the Java platform with many features that are inspired by languages like Python, Ruby and Smalltalk, making them available to Java developers using a Java-like syntax."

With Groovy, you can develop shell scripts, automated unit-tests, prototypes, and full-blown web applications with ease. Groovy works with all existing Java objects and libraries and compiles straight to Java bytecode in either application development or scripting mode.

Here is an example of a 100% Groovy script (taken from Groovy’s site):

class Greet {
def name
Greet(who) {
name = who[0].toUpperCase() + who[1..-1]
}
def salute() {
println "Hello $name!"
}
}
g = new Greet('world') // create object
g.salute() // Output "Hello World!"


Notice the omission of semicolons? Groovy was designed to be less wordy and easier to type, and this is one example of it. String manipulation is also intended to be easier than with Java, as shown in the above example.

The example below illustrates how you can use Groovy with Java to make life even easier (again, taken from Groovy’s site):

import org.apache.commons.lang.WordUtils
class Greeter extends Greet {
Greeter(who) {
name = WordUtils.capitalize(who)
}
}
new Greeter('world').salute()


It wasn’t too long ago that I read blogs, articles, and even whole books on how Ruby was going to replace Java. While these publications raised very good points about some of the shortcomings of Java, I see a different trend occurring. Instead of dynamic languages like Ruby and Python replacing Java, I continue to see more cases where they are used with Java to build on the strengths both sides have to offer. Groovy is a good example of that, along with Java SE 6, which has built-in support for scripting (JSR-223), and even ships with the Rhino JavaScript engine.

Tell me if you’ve seen the benefits of integrating dynamic languages with Java, or have instead chosen one side over the other. Email me at eric@ericbruno.com

-EJB

Posted by Eric Bruno at 12:56 PM  Permalink




 
INFO-LINK