可能重复:
"else if"是否比"switch()case"更快?
Java中if/else与switch语句的相对性能差异是什么?
我知道case语句可以用跳转表来实现.这是否比if语句更有效?
这只是应该避免的微观优化吗?
我需要检查一小部分逻辑,如果有人能给我一些宝贵的意见,我将非常感激.
我有两种检查逻辑的方法,想知道哪种方法更有效.
第一种方式:
if(url.equalsIgnoreCase("1")){
url = "aaa";
}
else if(url.equalsIgnoreCase("2")){
url = "bbb";
}
else if(url.equalsIgnoreCase("3")){
url = "ccc";
}
else if(url.equalsIgnoreCase("4")){
url = "ddd";
}
else if(url.equalsIgnoreCase("5")){
url = "eee";
}
else if(url.equalsIgnoreCase("6")){
url = "fff";
}
Run Code Online (Sandbox Code Playgroud)
第二种方式:
int temp = Integer.parseInt(url);
switch (temp) {
case 1:
url = "aaa";
break;
case 2:
url = "bbb";
break;
case 3:
url = "ccc";
break;
case 4:
url = "ddd";
break;
case 5:
url = "eee";
break;
case 6:
url = …Run Code Online (Sandbox Code Playgroud)