9, operator overload
l Groovy supports operator overload, making numeric, collection, MAP, and other types of data structures easier to use.
l The normal method of various operators in Groovy is mapped to the object being called.
Operator
Method
A B
A.Plus (b)
A - B
A.minus (b)
A * B
A.Multiply (b)
A / B
A.DIVIDE (B)
A OR A
a.next ()
A - or --a
A.PREVIOUS ()
a [b]
A.Getat (b)
a [b] = c
A.Putat (B, C)
a << b
a.leftshift (b)
a == B
a.equals (b)
a! = b
! a.Equals (b)
a === b
A == B in Java
a <=> B
A.compareto (B)
A> B
A.compareto (b)> 0
a> = b
A.compareto (b)> = 0
A
A.compareto (b) <0
a <= b
A.compareto (b) <= 0
l Note: All comparison operators have processed NULL to avoid throwing java.lang.nullpointerException
a = null
B = "foo"
askERT A! = B
askERT B! = A
askERT A == NULL
l Prior to different types of numerical comparisons, Groovy automatically converts the type of value into a larger range of numerical types, so the following example is effective:
BYTE A = 12
Double B = 10
Assert a instanceof byte
askERT B InstanceOf Double
askERT A> B