Groovy has used the ==
operator to check if objects are equal for a long time. To test if object instances are the same we must use the is
method. Groovy 3 adds a new operator for the is
method and that is the ===
operator. And to negate the result of the is
method we can use the !==
operator.
In the following example we use the ===
and !==
operator to check if objects refer to the same instance or not:
1 2 3 4 5 6 7 8 9 10 11 12 | def answer = new Integer( 42 ) def anotherAnswer = new Integer( 42 ) def meaningOfLife = answer // Equals is true for answer and anotherAnswer assert answer == anotherAnswer // But they don't refer to the same instance. assert answer !== anotherAnswer // The variables meaningOfLife and answer // do refer to the same object instance. assert answer === meaningOfLife |
Written with Groovy 3.0.2.