<分区>
我正在为我的 Android 应用程序用 Java 编写一些简单的代码,但出现了这些错误。
case 表达式必须是常量表达式
而private static final Integer
是常量
private static final Integer INVALID_USER = 901;
private static final Integer SENDING_FAILED = 902;
private static final Integer OK = 903;
/*
*
* And some more project related declaration...
*
*/
switch (responseCode){
case INVALID_USER:
// logout
break;
case SENDING_FAILED:
//resend request
break;
case OK:
break;
}
这是因为我使用了Integer Type
,然后我将类型更改为int
,问题就解决了
我的问题是为什么我们不能使用 Integer
作为 case 表达式。 Docs says " A switch works with the byte, short, char, and int primitive data types. It 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"虽然变量是常数
我读了this question但没有得到任何东西