Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.
Trending News
How would you rewrite this using a switch statement?
Given this method: Java
String checkZero(int x)
{
String s;
if (x == 0)
s = "zero";
else
s = "non-zero";
return s;
}
1. How would you rewrite this using a switch statement?
2. Which do you think is the best choice for this method? Why?
1 Answer
- SídheLv 71 decade agoFavorite Answer
String s;
switch (x) {
case 0: s = "zero"; break;
default: s="non-zero";
}
return s;
2. If-else is better in this case. Swtich is better when the alternative would be if-else if-else if-else if-else if-else if-else if-else (that is, a LOT of cases)