-2

I am a beginner, and I use the switch statement a lot. Sometimes it becomes quite annoying to use it. Are there any other statements in Java that can do a similar job like the switch statement?

ViceroyFaust
  • 162
  • 1
  • 1
  • 12
  • You may well be able to refactor your code to reduce the amount of switches. It is impossible to say since there's no indication here of what your code does. If you want something that can drop-in and replace any switch statement -- how would the result be less annoying, if it worked identically? – khelwood May 09 '17 at 13:17
  • Yes ,why not you can use if -else-if(ladder if) statement but switch case is the fastest of all. – bit-shashank May 09 '17 at 13:17
  • 1
    Possible duplicate of [Alternative to Switch Case in Java](http://stackoverflow.com/questions/1425659/alternative-to-switch-case-in-java) – Mario Santini May 09 '17 at 13:17
  • 1
    Polymorphism was invented to get rid of switch statements. – duffymo May 09 '17 at 13:29

4 Answers4

1

A drop-in alternative is a block of if, else if, else.

This can be preferable to a switch if the corresponding case expressions are not compile-time evaluable. Purists like me dislike switching on strings in Java, and C# seems to be moving to a paradigm where you can switch on pretty much anything.

You can also put the more frequently occurring cases towards the top of an if block, and knock out conditions early on that could cause later tests to fail, such as checking objects for null.

You can also consider using a chain of ternary conditional, but note that they have curious type promotion rules in Java.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
1

you can use an if-structure

if (condition) {
  // do this
} else if (condition2) {
  // do this
} else {
  // do something else
}

You can leave out the else-if or else part if you like

M. Haverbier
  • 383
  • 2
  • 13
1

Actually it depends on situation if you have only two cases then you can go for the Ternary operator ('?').
But Yes, there is alternative you can do refactoring by using polymorphism and inheritance. Like this,

    // *** With switch ***
    class Car {
      //...
      double getSpeed() {
        switch (type) {
          case BMW:
            return 250;
          case AUDI:
            return 200;
          case SUZUKI:
            return 300;
        }
      }
    }



    // *** Without switch *** Create Abstract class
    abstract class Car {
      abstract double getSpeed();
    }

    class BMW extends Car {
      double getSpeed() {
        return 250;
      }
    }
    class Audi extends Car {
      double getSpeed() {
        return 200;
      }
    }
    class suzuki extends Car {
      double getSpeed() {
        return 300;
      }
    }


    // simple use
    speed = Car.getSpeed();
Krishnan
  • 66
  • 10
0

There are many ways to achieve things - it highly depends on the situation. The obvious one is if-else, but sometimes, a Map is a better option.

See this question: How is this design pattern called? (map instead of switch)

Community
  • 1
  • 1
Tobi
  • 182
  • 8
  • If you are switching on a non-negative `int`, you may sometimes use an array instead of the map. It’s not easy to keep the initialization of the array readable and clear, though. – Ole V.V. May 09 '17 at 13:33