出于各种商业原因,我想在我的一个类中保留一些静态ID.它们最初是int但我想将它们更改为Integer所以我可以对它们做一个等号(即避免NPE的MY_ID.equals(..))
当我将它们更改为Integer时,我的switch语句中出现错误. docs说Integer在交换机中应该没问题.
去引用
[Switch] also works with enumerated types (discussed in Enum Types),
the String class, and a few special classes that wrap certain
primitive types: Character, Byte, Short, and Integer (discussed in
Numbers and Strings).
在下面的代码中,如果我是一个int,那么它编译.当它是一个整数时,它并不是说需要一个常量表达式.我尝试过.intValue(),但这也不起作用.
我真的很蠢吗?还是完全误读了docs?
private static final Integer i = 1;
@Test
public void test() {
switch(mObj.getId()){
case i: //do something
default: //do something default
}
}
感谢您的任何指示.暂时我将它们保持为int并执行新的Integer(myint).equals(…)