在此字符串中,Eclipse 在“不匹配”上显示死代码警告?
String b = ("goodString")==("goodString") ? "Condition Macth": "Not Match";
它在编译之前是否检查字符串?
String a = ("goodString".equals("goodString")) ? "Condition Macth" : "Not Match";
如果我更改为 .equals 则警告关闭。
最佳答案
Eclipse 可能会分析您的代码以检测死代码。
在这种特殊情况下,它会显示警告,因为这些值始终具有相同的标识,因为 string interning ,因此条件始终为真。 Eclipse 检测到这一点并发出警告。
如果您使用equals
,则比较不再是常量表达式 ( JLS § 15.28 ),并且不再保证返回true
。因此警告消失了。
顺便说一句,您应该始终使用equals
来比较字符串。
关于java - eclipse在编译前比较静态值吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54340615/