tags:

views:

267

answers:

4

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.

+1  A: 

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
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!
MatrixFrog
"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.
MatrixFrog
Don't forget `String.intern()`. Interned strings can be equivalently compared by reference or value.
finnw
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
+6  A: 

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.

fastcodejava
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.
MatrixFrog
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
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
"Probably can" is not really an answer (and Findbugs doesn't detect this).
Pascal Thivent
-1 for the previously given reason BTW (forgot to mention that I downvoted)
Pascal Thivent
PMD does it: http://pmd.sourceforge.net/rules/index.html#String_and_StringBuffer_Rules (UseEqualsToCompareStrings rule)
MatrixFrog
+6  A: 

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

lbreisacher
+1, good find!!
fastcodejava
A: 

This is correct question however Eclipse can warn you when you have variable, you are not using probably - "Local variable is never read" and this is also 'valid Java language feature' to have such variables.

Betlista
I think you meant this as a comment, not a new answer. But yes, exactly. It's VALID to do that, but it's also sensible for Eclipse to warn you, since it PROBABLY indicates a mistake. Same with using "==" for Strings.
MatrixFrog