Groovy Syntax Compared to Java
Site Admin
· 11 Sep 2026
· 7 views
Simplified Syntax
Groovy takes Java's syntax and makes it more concise. The result is code that is shorter, easier to read, and faster to write.
Key Syntax Differences
// Java
class Person {
private String name;
public Person(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
// Groovy
class Person {
String name // Auto-generates getter/setter
}
Properties and Getters/Setters
When you declare a property in Groovy, the compiler automatically generates a private field, a getter, and a setter. You can access properties directly:
def person = new Person(name: "Alice")
println person.name // Uses auto-generated getter
person.name = "Bob" // Uses auto-generated setter
Optional Parentheses
Groovy allows you to omit parentheses in method calls when there is a single argument:
// These are equivalent
println("Hello")
println "Hello"
// And for no arguments
toString()
toString()
String Interpolation
Groovy supports GStrings, which allow variable interpolation directly in strings using the dollar sign:
def name = "World"
def greeting = "Hello, ${name}!"
println greeting // Hello, World!
Key Points
- Groovy eliminates boilerplate with property auto-generation.
- Parentheses are optional in many method call scenarios.
- GStrings support variable interpolation with
${}. - Groovy is a superset of Java - valid Java is valid Groovy.
- Default access modifiers replace public/private in many cases.