One of the very nice features of Groovy is that we can implement operator overloading. This blog post is not about how to implement operator overloading, but Groovy's operator overloading also means that operators we know in Java have corresponding methods, which are not available in Java. So instead of using operators in our code we can use the corresponding methods.

The following sample code shows some operators we know in Java and the corresponding methods in Groovy:

def a = true
def b = false

assert a | b
// Java Boolean has no or method.
assert a.or(b)

assert !(a & b)
assert !(a.and(b))

def x = 100
def y = 10

assert x + y == 110
// Java Integer has no plus method.
assert == 110

assert ++x == 101
// ++ maps to next method.
assert == 102

assert --y == 9
assert y.previous() == 8

Written with Groovy 2.4.4.

Original article