Groovy Truth
last modified October 15, 2026
In Groovy, any value can be used where a boolean is expected, such as in an
if condition or the operands of && and
||. Groovy converts the value with a set of rules called Groovy
truth. This tutorial shows those rules, how to use them safely, and how to
customize them for your own classes.
The Basic Rules
Null, false, zero, empty strings, empty collections, and empty maps are false. Non-empty strings, non-zero numbers, and non-empty collections are true. Any object that is not one of these special cases is true, unless its class defines a different rule.
def values = [null, false, 0, "", [], [:], "text", 42, [1], [a: 1]]
values.each { v ->
println "${String.valueOf(v).padRight(8)} -> ${v ? 'true' : 'false'}"
}
The ternary operator ? : uses Groovy truth. The output shows that
only null, false, zero, and the empty values are false.
This makes checks such as if (name) concise for optional inputs.
Using Groovy Truth in Conditions
Instead of comparing with null and the empty string separately,
test the value directly. Be careful with numbers: 0 is false, so
if (count) does not distinguish a missing count from zero.
def describe(String name) {
if (name) {
return "Hello, $name"
}
return "Hello, guest"
}
println describe("Ann")
println describe("")
println describe(null)
def count = 0
if (!count) {
println "No items, or count was never set"
}
An empty string and null both fall through to the default greeting.
The last check treats 0 and null the same way, which
is exactly the ambiguity to watch for. When the difference matters, compare
explicitly with count == null.
Logical Operators and Values
The &&, ||, and ! operators use
Groovy truth, but they always return a boolean. To get one of the values back,
use the Elvis operator ?:, which returns its left operand when that
operand is true and its right operand otherwise.
def user = [name: "", nick: "Ace"] println user.name || user.nick println user.name ?: user.nick println user.missing ?: "no value"
user.name || user.nick is true, because the nick is
not empty. The Elvis operator returns the value itself: the empty name is skipped
and "Ace" is printed. Use ?: when you need a value, and
|| when you need a yes-or-no answer.
Groovy Truth for Custom Classes
Define an asBoolean() method to control how your objects are
evaluated. Groovy calls it whenever the object is used as a condition.
class Inventory {
List<String> items = []
boolean asBoolean() {
!items.isEmpty()
}
}
def stock = new Inventory()
println stock ? "In stock" : "Empty"
stock.items << "keyboard"
println stock ? "In stock" : "Empty"
Without the method, every Inventory object would be true. With
asBoolean(), the object is true only when it holds items, so it reads
like a collection.
Safe Checks with the Not Operator
The ! operator negates Groovy truth, so !list is true
for null and for an empty list. Use it for guard clauses at the
start of a method.
def average(List<Integer> numbers) {
if (!numbers) {
return null
}
numbers.sum() / numbers.size()
}
println average([2, 4, 6])
println average([])
println average(null)
Both the empty list and null return null from
average, so the division never runs with a zero divisor. The
guard reads more clearly than separate null and isEmpty()
checks.
Best Practices
- Use truth for presence checks:
if (name)is clear for strings and collections. - Be explicit with numbers: Compare with
== 0or!= nullwhen zero is a valid value. - Remember the return value:
&&and||return an operand, so do not assume a boolean. - Define asBoolean deliberately: Only add it when an object has a natural empty state.
Source
Groovy Semantics Documentation | Groovy Operators Documentation
Groovy truth keeps conditions short, but it hides the difference between
null, empty values, and zero. Use it for presence checks, and compare
explicitly when a value such as 0 carries meaning.
Author
List all Groovy tutorials.