43

I know that when comparing Strings, you should use .equals() not == and I understand the reasons for this. However I do sometimes forget and compare strings with == by mistake. I would like it if Eclipse (being much smarter than me), would warn me by doing one or more of the following:

  • Underlining my mistake with a wiggly red line and pointing out the mistake
  • Refusing to compile my code until I fix the mistake
  • Fixing the mistake for me
  • Beating me over the head with the nearest Joshua Bloch book until I apologize

In Eclipse 3.5, you can have Eclipse warn you about all kinds of things, by going to Window > Preferences > Compiler > Errors/Warnings, but sadly "Comparing strings with == instead of .equals()" does not seem to be one of them. Am I just missing it? Is there any chance of adding this in a future release?

EDIT: I'd rather do this using Eclipse's built-in functionality, rather than have to download a plugin. However, I think it would still be useful to mention plugins that have this feature in your answers.

Community
  • 1
  • 1
Tyler
  • 21,762
  • 11
  • 61
  • 90
  • 6
    +1, why are people downvoting this? Isn't this a real question? – SyntaxT3rr0r Feb 06 '10 at 07:02
  • 4
    @MatrixFrog: for what it's worth IntelliJ IDEA has been solving your problems 1 and 3 since a very long time. It even tells you: "String values are compared using '==', not '.equals()'" and for it supports programming by intention you hit alt+enter (or whatever your shortcut is) and it's fixed for you. If you're interested there's now an open source version of IntelliJ IDEA. – SyntaxT3rr0r Feb 06 '10 at 07:05
  • 1
    Let me know when you have a IDE which can beat programmers with a Joshua Bloch book. I'd be very interesting in that. :-) – JesperE Feb 06 '10 at 09:36
  • Jesper, I agree it would be nice if the IDE could say it instead of having programmers with books to review your code :) – Thorbjørn Ravn Andersen Feb 06 '10 at 10:24
  • You could write your own Eclipse plugin to warn you of things like this – Adrian Dec 20 '11 at 14:49
  • You can use Intellij idea. It suggests you that string should be compared with equals. – आनंद Jul 04 '17 at 09:50

4 Answers4

19

https://bugs.eclipse.org/bugs/show_bug.cgi?id=39095

Bug 39095 - RFE: warn when non-primitives are compared using ==

It would be nice if the compiler could allow warning when == is used to compare non-primitive types, so that String's compared with ==, while a valid approach if you're intern'ing and using the string pool, can be caught as compile-time warnings instead of run-time non-obvious breakages..

gnat
  • 6,213
  • 108
  • 53
  • 73
16

Eclipse can never tell you that. Findbugs or PMD probably can.

EDIT : I say

Eclipse can never tell you that.

because there nothing wrong with code a == b as java code. If you want some extra help, this is where other plugins come to help.

Edit: According to Pascal Thivent, Findbugs can't do this, but it looks like PMD can. I'm leaving the link to Findbugs, though, since it's probably a useful link for people who come across this question.

Tyler
  • 21,762
  • 11
  • 61
  • 90
fastcodejava
  • 39,895
  • 28
  • 133
  • 186
  • 2
    It looks like those are both plugins for Eclipse, so in fact Eclipse *can* tell me. I'd prefer not to download anything but that is an option. – Tyler Feb 06 '10 at 06:55
  • 3
    Saying that "Eclipse can never tell you that" is pretty meaningless. Neither Eclipse nor any plugin can **guarantee** that `a == b` is incorrect. It is a floating boundary between what the Eclipse compiler warns about and what external plugins can warn about. – JesperE Feb 06 '10 at 09:35
  • 3
    I'm not sure whether Findbugs tell this kind of error, but it shows a lot of similar ones (I seldom compare strings, and then I may remember to use .equals() or .contentEquals() – Zoltán Ujhelyi Feb 06 '10 at 12:43
  • 2
    "Probably can" is not really an answer (and Findbugs doesn't detect this). – Pascal Thivent Feb 06 '10 at 14:12
  • -1 for the previously given reason BTW (forgot to mention that I downvoted) – Pascal Thivent Feb 06 '10 at 14:32
  • 4
    PMD does it: http://pmd.sourceforge.net/rules/index.html#String_and_StringBuffer_Rules (UseEqualsToCompareStrings rule) – Tyler Feb 07 '10 at 08:43
  • 1
    2018 update - [Findbugs is dead](https://marketplace.eclipse.org/content/findbugs-eclipse-plugin) work has continued [in Spotbugs](https://marketplace.eclipse.org/content/spotbugs-eclipse-plugin). PMD has [moved to GitHub](https://pmd.github.io/). – Stuart Brock Jul 20 '18 at 09:05
0

Contrary to what has been said in earlier comments, FindBug (now?) can detect such comparison errors. In my case, FindBug was better able to report errors than PMD.

There are two bug report categories:

And I'd like to add to the debate of "Is this warning useful?":

I've converted a lot of JavaScript code from an HTML5 game to convert it to an Android game. In JavaScript, strings are compared with "==". They were converted that way to Java. So I needed a tool to be absolutely sure I replaced all by ".equals(".

Sebien
  • 821
  • 8
  • 11
-1

I dont think you can.

Using == is a valid java language feature. There would be circumstances where you would want to do this exact thing. Then what?

Probably, one fine day some programmer would want the IDE to warn him when he writes i = 10 (instead of i == 10)

Fortunately, this is where the human takes over the machine and we all have our jobs :)

Sadly, there are so many buggy systems :(

I am sure, over time you will get in the habit of using equals() for String

Mihir Mathuria
  • 6,479
  • 1
  • 22
  • 15
  • 4
    The only good reason I've ever seen for using == *with Strings* is to demonstrate that == and equals can potentially do different things. You're right about the habit though. I am pretty much in the habit, but I slip up occasionally. It was actually a copy-and-paste error that prompted me to ask this question, so maybe I should just be more careful with copy and paste! – Tyler Feb 06 '10 at 06:53
  • 4
    "Probably, one fine day some programmer would want the IDE to warn him when he writes i = 10 (instead of i == 10)". Um, Eclipse already does that. It's under "Potential programming problems" and it's called "possible accidental boolean assignment". I don't see why you seem think that's a bad thing. – Tyler Feb 06 '10 at 06:58
  • 4
    Don't forget `String.intern()`. Interned strings can be equivalently compared by reference or value. – finnw Feb 06 '10 at 07:03
  • 1
    IntelliJ does the very thing he is asking for with its inspections. So it is certainly possible. It does the equivalent of option 1 the asker gave (flags it in the code as a warning). – kenj0418 Feb 06 '10 at 07:10
  • It is a valid request ! See https://bugs.eclipse.org/bugs/show_bug.cgi?id=39095 from answer http://stackoverflow.com/a/2212548/281545 – Mr_and_Mrs_D Nov 21 '13 at 14:51