Groovy different results on using equals() and == on a GStringImpl

Nice question, the surprising thing about the code above is that

println "${'test'}".equals('test')

returns false. The other line of code returns the expected result, so let’s forget about that.

Summary

"${'test'}".equals('test')

The object that equals is called on is of type GStringImpl whereas 'test' is of type String, so they are not considered equal.

But Why?

Obviously the GStringImpl implementation of equals could have been written such that when it is passed a String that contain the same characters as this, it returns true. Prima facie, this seems like a reasonable thing to do.

I’m guessing that the reason it wasn’t written this way is because it would violate the equals contract, which states that:

It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.

The implementation of String.equals(Object other) will always return false when passed a GSStringImpl, so if GStringImpl.equals(Object other) returns true when passed any String, it would be in violation of the symmetric requirement.

Leave a Comment