java - 当我将字符串与 == 而不是 .equals() 进行比较时,如何告诉 Eclipse 警告我

标签 java eclipse

我知道 when comparing Strings, 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/

相关文章:

java - IKVM.NET 作为 OpenOffice 的 JRE

java - 错误的字符串值 - JOOQ - Java

java - JSON 教程/常见问题解答/示例/基础知识

java - 谁负责 MySQL 和 Hibernate 之间主键的自增?

java - 如何在 Mac 上使用终端从 Eclipse 编译 Java 程序

java - onBackpressed() 中的问题

eclipse - 结对编程、混合 IDE 环境?

java - 无法看到如何告诉 Maven 在 lib 文件夹上安装 jar

java - 扩展 Eclipse View 部分的选项卡上下文菜单

java - Eclipse - glassfish 无法启动,因为 org.xml.sax.SAXParseException;