2

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.

Community
  • 1
  • 1
Dan King
  • 3,412
  • 5
  • 24
  • 23
  • If I answered to your question directly, I'd get flagged as `rude/offensive` :/ – xenteros Sep 02 '16 at 10:54
  • @Tunaki - can you please explain how this is a duplicate? I understand the difference between == and equals() - I think I've already made that clear in my question? My question was about the reason for the PMD rule that seems to be trying to prevent me from using '==' at all, when - it seems to me - it is often perfectly legitimate to do so. – Dan King Sep 02 '16 at 11:05
  • @xenteros I am sorry if you were offended by my question. Is it possible you can give me a toned down version of your response and explain why you have a problem with it? – Dan King Sep 02 '16 at 11:06
  • You should never compare Objects with `==` but with `equals`, this is what the warning is telling you. The difference and explanation of those 2 ways is explained in the linked question. – Tunaki Sep 02 '16 at 11:07
  • @Tunaki. Where does it say in the linked question that you should never compare Objects with `==`? Why should you never do that? This is the question I am trying to get an answer to. I understand exactly what it does - that's why I want to use it! – Dan King Sep 02 '16 at 11:10
  • 1
    The reason from the linked question is *Thus you will be testing for object equality and not functional equality*. And it is true that comparing Objects with `==` is, more often than not, a bug. This is very likely the reason why the PMD rule exists ([see this question](http://stackoverflow.com/questions/2509856/the-why-behind-pmds-rules)). To disable the warning, check [this question](http://stackoverflow.com/questions/14702932/eclipse-duplication-annotation-surpresswarnings-for-pmd). – Tunaki Sep 02 '16 at 16:28

2 Answers2

2

In your case, you know what you are doing, and need to compare reference so for sure the rule does not apply. And you have to use ==.

But most of the time, that's a mistake from new Java developers who try to compare value of objects using == instead of .equals().

YMomb
  • 2,366
  • 1
  • 27
  • 36
0

Addition to @YMomb:

PMD and those kind of static analysis tools always leave the final decision to user. You have full rights to ignore any rule if you believe that your design is correct.

oddi
  • 91
  • 7
  • That's true Arda, but if you're working on a large project which has targets for the extent to which the code satisfies the code analysis tools, and you don't personally have control over the rule set then it becomes more difficult (in this case I just used @SuppressWarnings, but my project discourages this). Basically though, I was just curious to know the justification for this rule. This particular one seems rather heavy-handed IMO. – Dan King Sep 14 '16 at 13:40