Java if else 或 switch case

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18653051/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 09:51:51  来源:igfitidea点击:

if else or switch case

java

提问by Sushil

I need to check a small piece of logic and would highly appreciate if someone can give me some valuable input.

我需要检查一小部分逻辑,如果有人能给我一些有价值的意见,我将不胜感激。

I have two ways of checking my logic and want to know which is more efficient.

我有两种检查逻辑的方法,想知道哪种方法更有效。

1st way:

第一种方式:

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";
}

2nd Way:

方式二:

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 = "fff";
    break;
}

Please let me know which is more efficient. Is it bad to use Integer.parseInt(string)?

请让我知道哪个更有效。用起来不Integer.parseInt(string)好吗?

采纳答案by njzk2

If your values really are 1-6, the clearest and most efficient way is using an array :

如果您的值确实是 1-6,那么最清晰、最有效的方法是使用数组:

String[] URLS = {...};
url = URLS[Integer.parseInt(url) - 1];

回答by christopher

As a general rule, the switchstatement produces more efficient bytecode. With Java 7, switch statements with Stringwas introduced, so you don't need to cast it.

作为一般规则,该switch语句产生更有效的bytecode。With Java 7, switch 语句 withString被引入,所以你不需要强制转换它。

回答by Philipp Sander

Please let me know which is more efficient.

请让我知道哪个更有效。

a switch statement is more efficient is your case

switch 语句更有效是你的情况

Is it bad to use Integer.parseInt(string)?

使用 Integer.parseInt(string) 是不是不好?

No. it's fine. but when you're using java7 you can use String-constants values in your switch cases but not on Android.

不,还好。但是当您使用 java7 时,您可以在 switch case 中使用 String-constants 值,但不能在 Android 上使用。

Asside from the efficiency: switch looks cleaner in most cases.

除了效率之外:在大多数情况下,switch 看起来更干净。

回答by Nargis

In terms of efficiency check this: Case vs If Else If: Which is more efficient?, but Way 2 looks to be a more clean and readable code.

在效率方面,请检查:Case vs If Else If:哪个更有效?,但方式 2 看起来是一个更干净和可读的代码。

回答by JavaDM

for such a long list is a switchstatement definitely the better choice. The longer the junction is if the better cut off a switch in comparison

对于这么长的列表,switch声明绝对是更好的选择。相比之下,如果更好地切断开关,则结越长

回答by P Ravikant

In this case, switchis more efficient.

在这种情况下,switch效率更高。

In fact, If you are using Java7, you may directly use string case rather than using Integer.parseInt().

事实上,如果你使用的是 Java7,你可以直接使用字符串大小写而不是使用Integer.parseInt().

回答by GerritCap

It is not bad using parseInt but it will throw an exception if the string is not an integer.

使用 parseInt 还不错,但如果字符串不是整数,它会抛出异常。

otherwise, and I think you can see this for yourself, the switch/case is much more readible.

否则,我认为您可以亲眼看到这一点,开关/外壳更易读。

if your if construct would have a final else catching all other cases (including non numeric strings)

如果您的 if 构造将有一个最终的 else 捕获所有其他情况(包括非数字字符串)

then you can do, assuming your ints are always positive

那么你可以做,假设你的整数总是积极的

int temp = -1;
try {
    temp = Integer.parseInt(str);
} catch (NumberFormatException ex) {
    // ignore exception and use -1 as original value for default case
}

回答by NCA

Efficiency depends on the matching condition in both cases.Please conform your answer here.

效率取决于两种情况下的匹配条件。请在此处确认您的答案。

回答by Rakesh KR

Readability and debugability (is that even a word?) are rather subjective but some people (including me) find the switchstatement to be more clear. However, in many cases a compiler has a better chance of generating faster code using a switchcompared to the if elseconstruct.

可读性和可调试性(甚至是一个词?)是相当主观的,但有些人(包括我)发现这switch句话更清楚。但是,在许多情况下switch,与if else构造相比,编译器更有可能使用 a 生成更快的代码。

回答by tea2code

I think an alternative approach would be to use a pre initialized map. It should have the (string)numbers as the key and the url results as value. Then you can simply do

我认为另一种方法是使用预先初始化的地图。它应该将(字符串)数字作为键,将 url 结果作为值。然后你可以简单地做

url = map.get(Integer.parseInt(url));

This is probably even a much cleaner version then the long switch statement.

这可能是一个比长 switch 语句更简洁的版本。