简体   繁体   中英

How to Change If-else statement into Switch Case?

How to change my if-else statement into Switch Case structure?

if (apiName.equals(APIUrlConstant.getIsRegistrationenabledUrl())) {
    IsRegistrationEnableModel isRegistrationEnableModel = gson.fromJson(responsedata, IsRegistrationEnableModel.class);
    listener.onTaskPostExecute(isRegistrationEnableModel, requestData, responsedata);
} else if (apiName.equals(APIUrlConstant.AboutUs)) {
    // do Something
}else if (apiName.equals(APIUrlConstant.AddContentRating)) {
    // do Something
}

apiName is a String type. How to do that?

In the JDK 7 release, you can use a String object in the expression of a switch statement.

https://docs.oracle.com/javase/8/docs/technotes/guides/language/strings-switch.html

Actually you can't. Switch case expressions must be constant expressions and APIUrlConstant.getIsRegistrationenabledUrl() is not a constant expression.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM