Step‑by‑Step Evolution of a Hello World Program from Java to Groovy
This tutorial demonstrates how a simple Java Hello World class can be progressively transformed into idiomatic Groovy code, covering eight evolution stages that introduce Groovy’s syntax shortcuts, GString interpolation, dynamic typing, and script‑style execution while preserving the original program logic.
Groovy and Java are closely related languages with very similar syntax, so Java developers can learn Groovy easily; most Java programs are also valid Groovy programs simply by renaming the file extension from .java to .groovy .
The article starts with a basic Hello World Java program and shows how it evolves into Groovy through a series of incremental transformations.
Original Hello World
public class HelloWorld {
private String name;
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public String greet() {
return "Hello " + name;
}
public static void main(String[] args) {
HelloWorld helloWorld = new HelloWorld();
helloWorld.setName("Groovy");
System.out.println(helloWorld.greet());
}
}The class defines a private name field with its getter and setter, a greet() method that returns the greeting string, and a main() method that creates an instance, sets the name, and prints the greeting.
Groovy Program – First Evolution
public class HelloWorld {
private String name;
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public String greet() {
return "Hello " + name;
}
public static void main(String[] args) {
HelloWorld helloWorld = new HelloWorld()
helloWorld.setName("Groovy")
System.out.println(helloWorld.greet());
}
}At this stage the Groovy code is identical to the Java version; no Groovy‑specific features have been applied yet.
Groovy Program – Second Evolution
We begin “Groovy‑ifying” the program by removing semicolons and the public keyword, which are optional in Groovy.
class HelloWorld {
private String name
void setName(String name) {
this.name = name
}
String getName() {
return name
}
String greet() {
return "Hello " + name
}
static void main(String[] args) {
HelloWorld helloWorld = new HelloWorld()
helloWorld.setName("Groovy")
System.out.println(helloWorld.greet())
}
}Groovy Program – Third Evolution
Groovy’s GString allows embedding variables directly in double‑quoted strings, so the greet() method can use "Hello ${name}" instead of concatenation.
String greet() {
return "Hello ${name}"
}Groovy Program – Fourth Evolution
When the last expression in a method is the value to return, the return keyword can be omitted, making the method even shorter.
class HelloWorld {
private String name
void setName(String name) {
this.name = name
}
String getName() {
name
}
String greet() {
"Hello ${name}"
}
static void main(String[] args) {
HelloWorld helloWorld = new HelloWorld()
helloWorld.name = "Groovy"
System.out.println(helloWorld.greet())
}
}Both getName() and greet() are now single‑line methods.
Groovy Program – Fifth Evolution
Groovy automatically provides a private field and its getter/setter for a simple property declaration, so we can write String name without explicitly defining the field.
class HelloWorld {
String name
String greet() {
"Hello ${name}"
}
}
def helloWorld = new HelloWorld()
helloWorld.name = "Groovy"
println helloWorld.greet()Groovy Program – Sixth Evolution
Groovy adds convenient shortcuts such as using println instead of System.out.println() and allowing omission of parentheses for top‑level method calls.
class HelloWorld {
String name
String greet() {
"Hello ${name}"
}
static void main(String[] args) {
HelloWorld helloWorld = new HelloWorld()
helloWorld.name = "Groovy"
println helloWorld.greet()
}
}Groovy Program – Seventh Evolution
Groovy supports dynamic typing, so we can replace explicit types with def and even omit the return type of main .
class HelloWorld {
def name
def greet() {
"Hello ${name}"
}
static main(args) {
def helloWorld = new HelloWorld()
helloWorld.name = "Groovy"
println helloWorld.greet()
}
}Groovy Program – Eighth Evolution
Because Groovy is also a scripting language, we can drop the main method entirely and write a script that directly creates an instance and prints the greeting.
class HelloWorld {
def name
def greet() {
"Hello ${name}"
}
}
def helloWorld = new HelloWorld()
helloWorld.name = "Groovy"
println helloWorld.greet()The article concludes with a disclaimer about original publication and a list of other technical articles.
FunTester
10k followers, 1k articles | completely useless
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.