java 比较字符串和枚举

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10553649/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 01:32:20  来源:igfitidea点击:

comparing string with enumeration

java

提问by comatose

I am analyzing the following piece of code using a static analysis tool called FindBugs.

我正在使用名为 FindBugs 的静态分析工具分析以下代码段。

if(str.equals(enum.SOMEVALUE)) {// do something};

where str is a String and enum is an enumeration. The tool generates the following warning for this code, and states

其中 str 是一个字符串, enum 是一个枚举。该工具为此代码生成以下警告,并指出

This method calls equals(Object) on two references of different class types with no common subclasses. According to the contract of equals(), objects of different classes should always compare as unequal; therefore, according to the contract defined by java.lang.Object.equals(Object), the result of this comparison will always be false at runtime.

此方法在没有公共子类的不同类类型的两个引用上调用 equals(Object)。根据equals()的约定,不同类的对象总是比较不相等;因此,根据java.lang.Object.equals(Object)定义的契约,这个比较的结果在运行时总是false。

if I replace the above line of code with this:

如果我用这个替换上面的代码行:

if(str.equals(enum.SOMEVALUE.toString())) {// do something};

then the warning disappears.But I am not sure if the warning that the tool generates is really true and whether I am fixing it the right way ? because I've seen such comparisons before and it appears to be working correctly.

然后警告消失了。但我不确定该工具生成的警告是否真的正确,以及我是否以正确的方式修复它?因为我以前看过这样的比较并且它似乎工作正常。

回答by Tomasz Nurkiewicz

Your first comparison is basically wrong. You are comparing objects of completely different types (Stringand Enum) and they can never be equal. intellijeven gives me a warning here. It compiles only because equals()accepts Object, not a String.

你的第一次比较基本上是错误的。您正在比较完全不同类型(StringEnum)的对象,它们永远不可能相等。intellij甚至在这里给我一个警告。它编译只是因为equals()accepts Object,而不是 a String

The second comparison is correct.

第二个比较是正确的

Although JavaDoc is a bit harsh on name()method, I would actually advice using it in case given enum has toString()overriden:

尽管JavaDoc 对name()方法有点苛刻,但我实际上建议使用它,以防给定枚举已被toString()覆盖:

if(str.equals(FooEnum.SOMEVALUE.name()))

回答by Alex Stybaev

you can try

你可以试试

enum.SOMEVALUE.name()

as it

因为它

Returns the name of this enum constant, exactly as declared in its enum declaration.

返回此枚举常量的名称,与在其枚举声明中声明的完全相同。

回答by Juan Alberto López Cavallotti

I think replacing the constant for the toString()may be the right thing to do, I would change it for .name()though because toString is to be overriden.

我认为替换常量toString()可能是正确的做法,但我会更改它,.name()因为 toString 将被覆盖。

回答by Bozho

I'd suggest using:

我建议使用:

if (SomeEnum.SOMEVALUE == SomeEnum.valueOf(str)) {

}

回答by sarwar026

As far as I know, you are in the right path.

据我所知,你走在正确的道路上。

if(str.equals(enum.SOMEVALUE.toString())) {// do something};

This should be okay.

这应该没问题。