Until now, my understanding was that == is an operator overload for .equals(). However, I recently discovered that
new Integer(1) == new Long(1) // returns true
whereas
new Integer(1).equals(new Long(1)) // returns false
so I guess == is not exactly a shorthand for .equals(), so how does it determine equality?
==in Groovy is roughly equivalent toequals(), however, you’ll find it’s different from Java when comparing different classes with the same value – if the class isComparable. Groovy also does type casting if possible.If you check out the code, it looks like ultimately
compareToWithEqualityCheck()is executed for==.