我知道 when comparing String
s, you should use .equals()
not ==
我理解其中的原因。但是,我有时会错误地忘记并将字符串与 ==
进行比较。如果 Eclipse(比我聪明得多)会通过执行以下一项或多项操作来警告我,我希望它:
- 用一条 Swing 的红线强调我的错误并指出错误
- 在我修复错误之前拒绝编译我的代码
- 帮我解决错误
- 用最近的 Joshua Bloch 书殴打我,直到我道歉
在 Eclipse 3.5 中,您可以让 Eclipse 向您发出各种警告,方法是转到 Window > Preferences > Compiler > Errors/Warnings,但遗憾的是“使用 == 而不是 .equals() 比较字符串”似乎没有成为其中之一。我只是想念它吗?是否有机会在未来的版本中添加此功能?
编辑:我宁愿使用 Eclipse 的内置功能来执行此操作,而不必下载插件。但是,我认为在您的答案中提及具有此功能的插件仍然很有用。
最佳答案
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..
关于java - 当我将字符串与 == 而不是 .equals() 进行比较时,如何告诉 Eclipse 警告我,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2212307/