switch integer java,在java中的Switch语句中使用Integer类型

I was writing some simple code in java for my android app and i got these errors.

case expressions must be constant expressions while private static final Integer is constant

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;

}

This is because i used Integer Type, then i changed type to int and problem is solved

解决方案

Constant expressions are used as case labels in switch statements (

§14.11 ) and have a special significance for assignment conversion (

§5.2 ) and .....

A compile-time constant expression is an expression denoting a value

of primitive type or a String that does not complete abruptly.

Now in above scenario compiler is looking for constant expression as it should be know to the compiler at the compile time.As stated Integer is actually not a constant Expression for the compiler.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值