2

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)?

Kashif Nazar
  • 20,775
  • 5
  • 29
  • 46
Sushil
  • 8,250
  • 3
  • 39
  • 71
  • 2
    why the 'equalsIgnoreCase' for comparing integers? – Scary Wombat Sep 06 '13 at 08:30
  • @micro.pravi .. its partially duplicate of your suggested link. I mainly wanted to know if adding Interger.parseInt() will make it more efficient or not..so only I posted a fresh question.. – Sushil Sep 06 '13 at 10:02

12 Answers12

30

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

String[] URLS = {...};
url = URLS[Integer.parseInt(url) - 1];
Philipp Sander
  • 10,139
  • 6
  • 45
  • 78
njzk2
  • 38,969
  • 7
  • 69
  • 107
14

Please let me know which is more efficient.

a switch statement is more efficient is your case

Is it bad to use 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.

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

Philipp Sander
  • 10,139
  • 6
  • 45
  • 78
4

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.

Community
  • 1
  • 1
Nargis
  • 4,687
  • 1
  • 28
  • 45
3

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

christopher
  • 26,815
  • 5
  • 55
  • 89
3

In this case, switch is more efficient.

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

Pramod Ravikant
  • 1,039
  • 2
  • 11
  • 28
2

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

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)

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
}
GerritCap
  • 1,606
  • 10
  • 9
2

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

Rakesh KR
  • 6,357
  • 5
  • 40
  • 55
2

The switch statement is faster for the following two reasons.

  1. The switch statement is generally faster than if-else-if construct because the control is directly transferred to the respective case. While in the cases of if-else-if, the all the checks are going to be performed to reach the first matching condition. For example, if you assign temp = 6 the switch statement will execute the respective block directly. The if-else-if construct will iterate through all the conditions.

  2. Calling the equalsIgnoreCase() is more costly than performing the equality check that happens at the background of the case matching.

Kashif Nazar
  • 20,775
  • 5
  • 29
  • 46
1

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

JavaDM
  • 851
  • 1
  • 6
  • 29
1

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

Community
  • 1
  • 1
NCA
  • 810
  • 1
  • 8
  • 19
1

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 = map.get(Integer.parseInt(url));

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

tea2code
  • 1,007
  • 1
  • 8
  • 15
1

The switch has more features than the if else, because you can fall through cases.

See for details: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html

class SwitchDemo2 {
    public static void main(String[] args) {

        int month = 2;
        int year = 2000;
        int numDays = 0;

        switch (month) {
            case 1: case 3: case 5:
            case 7: case 8: case 10:
            case 12:
                numDays = 31;
                break;
            case 4: case 6:
            case 9: case 11:
                numDays = 30;
                break;
            case 2:
                if (((year % 4 == 0) && 
                     !(year % 100 == 0))
                     || (year % 400 == 0))
                    numDays = 29;
                else
                    numDays = 28;
                break;
            default:
                System.out.println("Invalid month.");
                break;
        }
        System.out.println("Number of Days = "
                           + numDays);
    }
}

will output

Number of Days = 29

So it also depends on your implementation requirements which one you use.

René Link
  • 48,224
  • 13
  • 108
  • 140