Posts

Showing posts with the label switch

It's time to "switch" on Java

Anyone who has used a switch statement should be familiar with this in Java. private String getName (String input) { switch (input) { case "a" : return "A" ; case "b" : return "B" ; case "c" : return "C" ; default : return "D" ; } } Looks pretty straight forward at a glance, but is it really? What if I use enums instead? public enum InputTypes { A , B , C ; } Now the code would look like the following: private String getName (InputTypes input) { switch (input) { case A : return "A" ; case B : return "B" ; case C : return "C" ; default : return "D" ; } } But for any eager eye, it should be obvious that there is something fishy here: in the enum, I have exhausted all the options in that enum, why shou...