I've just fallen foul of the CompareObjectsWithEquals rule in PMD, because I've compared two object references using '==' instead of equals(), but I'm struggling to see why this is a problem and can't find any justification for this restriction.
I appreciate that Object.equals() compares references and therefore has the same effect, but I'm not using a raw Object, so I can't guarantee that method won't be overridden at some point somewhere in the hierarchy.
I want to do a reference comparison, and I want to be sure that this always will be a reference comparison. Why would PMD try to force me to call equals()?
Is it just me, or is this a really stupid rule??
Edited:
Just to be clear - I am not asking what the difference is between ==
and equals()
(as per What is the difference between == vs equals() in Java?) - I understand this perfectly. I am asking why PMD would force me to always use equals()
when the caller may legitimately want to ensure that a reference comparison is performed.