| By Jim Driscoll | Article Rating: |
|
| March 20, 2012 11:45 AM EDT | Reads: |
2,393 |
Before I start talking about using Groovy's capabilities to create a DSL (mostly in Java), let's take a few minutes to go over what Groovy is.
Groovy is a general purpose scripting language which runs on the JVM, and can largely be viewed as a superset of Java. Take the following program:
public class Hello {
String name;
public void sayHello() {
System.out.println("Hello "+getName()+"!");
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public static void main(String[] args) {
Hello hello = new Hello();
hello.setName("world");
hello.sayHello();
}
}
Is this a Groovy program or a Java program? Yes, it is - it will compile and run in both. This basic program is a bit wordy, and we can certainly do things more simply in Java, but this contains a number of patterns that you'd commonly see, including the use of the bean pattern, as well as the use of the main method to make the class executable via the java command line program. When we run it, it simply prints out "Hello world!", as is customary in these sorts of things.
To try this out in Java, you can use your favorite IDE. You can also use an IDE to try this in Groovy, but you may just want to use the groovyConsole program, which is available as part of the GDK. Download it now, and play along via cut and paste...
Now, as I said, Groovy is a rough superset of Java - one difference is that things are public by default. That means we could just as easily say the following:
class Hello {
String name;
void sayHello() {
System.out.println("Hello "+getName()+"!");
}
void setName(String name) {
this.name = name;
}
String getName() {
return name;
}
static void main(String[] args) {
Hello hello = new Hello();
hello.setName("world");
hello.sayHello();
}
}
Accessors and mutators are automatically created for your class variables, so we can also shorten it by taking those out:
class Hello {
String name;
void sayHello() {
System.out.println("Hello "+getName()+"!");
}
static void main(String[] args) {
Hello hello = new Hello();
hello.setName("world");
hello.sayHello();
}
}
Now we're getting somewhere. In Groovy, System.out.println can be shortened to just "println" as a convenience, so you can also say:
class Hello {
String name;
void sayHello() {
println("Hello "+getName()+"!");
}
static void main(String[] args) {
Hello hello = new Hello();
hello.setName("world");
hello.sayHello();
}
}
There's also a difference in how Groovy deals with String objects - double quote strings allow for variable substitution. There's also single quote strings, which do not:
class Hello {
String name;
void sayHello() {
println("Hello $name!");
}
static void main(String[] args) {
Hello hello = new Hello();
hello.setName('world');
hello.sayHello();
}
}
Groovy also allows dot notation to get and set fields of classes, just like Java. Unlike Java, this will actually go through the getter/setter methods (which, you'll recall, are automatically generated in our current example):
class Hello {
String name;
void sayHello() {
println("Hello $name!");
}
static void main(String[] args) {
Hello hello = new Hello();
hello.name = 'world';
hello.sayHello();
}
}
Typing information is also optional - instead of specifying a type, you can just use the keyword def. Use of def is mandatory for methods, but optional for parameters on those methods. You should also use def for class and method variables. While we're at it, let's take out those semicolons: They're optional in Groovy.
class Hello {
def sayHello(name) {
println("Hello $name!")
}
static def main(args) {
Hello hello = new Hello()
def name = 'world'
hello.sayHello(name)
}
}
OK, that's nice, but we can get this even shorter. Because Groovy is a scripting language, there's automatically a wrapping class (called Script, which will become very important to us later). This wrapping class means that we can get rid of our own wrapping class, as well as the main method, like so:
def sayHello(name) {
println("Hello $name!")
}
def name = 'world'
sayHello(name)
That's all for now, I'll do a second part to this later. But for now, if someone asks you if you know Groovy, you can now say "Sure, a bit".
Credit for the idea behind this post goes to Guillaume Laforge - I saw him give a variation of this as an intro to his talk at JavaOne, and loved it.
(As usual, this article is cross posted on my main blog site.)
Read the original blog entry...
Published March 20, 2012 Reads 2,393
Copyright © 2012 SYS-CON Media, Inc. — All Rights Reserved.
Syndicated stories and blog feeds, all rights reserved by the author.
More Stories By Jim Driscoll
Jim Driscoll has worked at Sun Microsystems for 12 years, working on such projects as the first version of Servlets (in the Java Web Server), and the initial implementation of Java 2, Enterprise Edition. He is currently a Senior Engineer working on the implementation of Java Server Faces, helping to integrate technologies such as AJAX and Comet into the new release.
- Cloud People: A Who's Who of Cloud Computing
- New Relic Q1 2013 Blazes Past Growth Targets and Reaches 40,000 Active Customer Accounts
- Cloud Expo New York: Delivering Digital Marketing on the Cloud
- Cloudant to Exhibit at Cloud Expo & Big Data Expo New York
- Cloud Expo New York: Rethink IT and Reinvent Business with IBM SmartCloud
- The Accessibility of the Cloud
- Learn How To Use Google Apps Script
- Cloud Expo New York: Basics of SSD Technology and Its Use in Cloud
- Cloud Expo New York: Real-Time Analytics Using an In-Memory Data Grid
- Cloud Expo NY: Best Practices for Delivering Oracle Database as a Service
- Cloud Expo New York: The Big Challenge of Big Data & Hadoop Integration
- Measuring the Business Value of Cloud Computing
- Cloud People: A Who's Who of Cloud Computing
- Cloud Expo New York: Best CIO Practices Shared from SHI’s Customers
- Examining the True Cost of Big Data
- Cloud Expo New York: How to Use Google Apps Script
- Software Defined Networking – A Paradigm Shift
- New Relic Q1 2013 Blazes Past Growth Targets and Reaches 40,000 Active Customer Accounts
- Cloud Expo New York: Why Big Data Is Really About Small Data
- Cloud Expo New York: Delivering Digital Marketing on the Cloud
- Small Cancers, Big Data, and a Life Examined
- Cloud Expo New York: Requirements of a Cloud Database
- Cloud Expo NY: Calculating the True Value of Industry-Specific Clouds
- Cloudant to Exhibit at Cloud Expo & Big Data Expo New York
- A Cup of AJAX? Nay, Just Regular Java Please
- Java Developer's Journal Exclusive: 2006 "JDJ Editors' Choice" Awards
- JavaServer Faces (JSF) vs Struts
- The i-Technology Right Stuff
- Rich Internet Applications with Adobe Flex 2 and Java
- Java vs C++ "Shootout" Revisited
- Bean-Managed Persistence Using a Proxy List
- Reporting Made Easy with JasperReports and Hibernate
- Creating a Pet Store Application with JavaServer Faces, Spring, and Hibernate
- Why Do 'Cool Kids' Choose Ruby or PHP to Build Websites Instead of Java?
- What's New in Eclipse?
- Where Are RIA Technologies Headed in 2008?

























