Differences from Java
Groovy language grammar is derived from Java grammar, with various enhancements in the form of specific constructs and allowing additional simplifications.
In most cases, the difference is optional, and writing the code in the way one would write Java would have no issues. The main exception to this is Strings.
This page will illustrate some of the more significant differences in an easy to compare manner.
You can read more about the differences on the official documentation.
Semicolons
Semicolons at the end of a line are optional.
Code.run();
Code.run()
Parentheses
Parentheses around parameters are optional when there is no ambiguity.
Code.run('example');
Code.run(Code.nested('example'));
println('hello');
Code.run 'example'
Code.run Code.nested('example')
println 'hello'
Variable Declaration
Prior to Java 10, you had to declare a variable using the variable type. After Java 10, using the keyword var
, you could declare a variable using var
without having to specify the type ahead of time.
In Groovy, you can use def
to accomplish the same thing.
String name = "hello";
int num = 5;
def name = "hello"
def num = 5
Arrays
In Java, arrays are declared via curly braces {}
surrounding the variable.
In Groovy, you can use the explicit array creation syntax as normal, and can also declare an array via using square braces []
and explicitly declaring it as an array. If you do not explicitly declare it as an array, using square braces []
will create a list instead.
int[] array0 = new int[] {1, 2, 3};
int[] array1 = {1, 2, 3};
def array0 = new int[] {1, 2, 3}
def array1 = [1, 2, 3] as int[]
int[] array2 = [1, 2, 3]
def list = [1, 2, 3] // this creates a list, not an array!
Lists
In Java, there is no shorthand way to create a list.
In Groovy, you can easily create a list using square braces []
, using commas ,
to separate the values.
List<Integer> list = new ArrayList<>();
list.add("example");
list.add("demo");
def list = ["example", "demo"]
Maps
In Java, there is no shorthand way to create a map.
In Groovy, you can easily combine the same square braces []
used to create lists with colons :
to delineate a key-value entry.
Map<Integer, String> map = new HashMap<>();
map.put(1, "example");
map.put(3, "demo");
def map = [1: "example", 3: "demo"]
Visibility
In Java, fields, classes, and methods that do not have an access specifier stated are package-private
by default, meaning they are only visible to other classes inside the package.
Classes
In Groovy, classes declared without an access specifier default to public
.
public class DemoClass {}
class DemoClass {}
Fields
In Groovy, fields declared without an access specifier are declared as a property, which creates a private
field and an associated getter and setter method. If the field is final, only a getter method will be created.
public class DemoClass {
private final String value = "value";
private String name = "example";
public getValue() {
return this.value;
}
public getName() {
return this.name;
}
public setName(String newName) {
this.name = newName;
}
}
class DemoClass {
final def value = "value"
def name = "example"
}
Methods
In Groovy, methods declared without an access specifier default to public
.
public class DemoClass {
public void foo() {
// example
}
}
class DemoClass {
void foo() {
// example
}
}
Return
When a function expects to return a specific value, simply writing a value of that type at the end of the function will cause that to be returned.
public class DemoClass {
public DemoClass foo() {
return this;
}
public String hello() {
return "hello";
}
public String test(boolean test) {
if (test) {
return "Test is true";
} else {
return "Test is false";
}
}
}
class DemoClass {
DemoClass foo() {
this
}
def hello() {
'hello'
}
def test(test) {
if (test) {
'Test is true'
} else {
'Test is false'
}
}
}
Strings
In Java, declaring using single quotes ''
only accepts a single character and creates a char
, and you must use double quotes ""
to create a normal String
.
In Groovy, there is no shorthand to declare a char
, declaring using single quotes ''
creates a normal String
, and using double quotes ""
creates a special object called a GString
, which functions as a normal String
object unless it contains a dollar sign $
, in which case it has specific formatting rules.
Learn more about this over on the specific Strings page.
char character = 'a';
String name = "example";
char character0 = 'a'
def character1 = (char) 'a'
def character2 = 'a' as char
String name0 = "example"
def name1 = 'example'
def name2 = "example"
def nameGString = "$name0" // will create a GString, not a String, and will replace with the variable referenced
def nameInvalid = '$name0' // will have the raw text provided
Omitting .class
When you want to refer to a specific class
object, you can omit the .class
suffix.
public class DemoClass {
public Class<?> getClass() {
return DemoClass.class;
}
}
class DemoClass {
def getClass() {
DemoClass
}
}
Repeating Variable Reference
Groovy has multiple ways of simplifying interacting with multiple methods or fields of the same object.
DemoClass obj = new DemoClass();
obj.sayHi();
obj.value = 1;
obj.name = 'hello';
obj.total = 100;
obj.clay = 'clay';
obj.current = 5;
obj.sayGoodbye();
DemoClass obj = new DemoClass()
obj.with {
sayHi()
value = 1
name = 'hello'
total = 100
clay = 'clay'
current = 5
sayGoodbye() // returns the value of sayGoodbye()
}
DemoClass obj = new DemoClass().with {
sayHi()
value = 1
name = 'hello'
total = 100
clay = 'clay'
current = 5
sayGoodbye()
it // returns 'it', with 'it' being declared via the creation of the closure
}
DemoClass obj = new DemoClass().tap {
sayHi()
value = 1
name = 'hello'
total = 100
clay = 'clay'
current = 5
sayGoodbye() // using tap will return the instance, not sayGoodBye()
}