Why can't I switch on a String in Java?
Is this functionality going to be put into a later Java version?
Can someone point me to an article, or themselves explain why I can't do this, as in, the technical way Java's switch statement works?
Why can't I switch on a String in Java? Is this functionality going to be put into a later Java version? Can someone point me to an article, or themselves explain why I can't do this, as in, the technical way Java's switch statement works? |
|||||
|
Switch statements with Implementation in JDK 7The feature has now been implemented in A Switches in the JVMFor more technical depth on If the constants are dense, they are used as an index (after subtracting the lowest value) into a table of instruction pointers—the If the constants are sparse, a binary search for the correct case is performed—the In de-sugaring a Both instructions require the integer constants assigned to each case to be sorted at compile time. At runtime, while the Before JDK 7Prior to JDK 7,
|
|||||||||||||||||||||
|
If you have a place in your code where you can switch on a String, then it may be better to refactor the String to be an enumeration of the possible values, which you can switch on. Of course, you limit the potential values of Strings you can have to those in the enumeration, which may or may not be desired. Of course your enumeration could have an entry for 'other', and a fromString(String) method, then you could have
|
|||||||||||||
|
The following is a complete example based on JeeBee's post, using java enum's instead of using a custom method. Note that in Java SE 7 and later you can use a String object in the switch statement's expression instead.
|
||||
Switches based on integers can be optimized to very efficent code. Switches based on other data type can only be compiled to a series of if() statements. For that reason C & C++ only allow switches on integer types, since it was pointless with other types. The designers of C# decided that the style was important, even if there was no advantage. The designers of Java apparently thought like the designers of C. |
|||||||||||||||||
|
James Curran succinctly says: "Switches based on integers can be optimized to very efficent code. Switches based on other data type can only be compiled to a series of if() statements. For that reason C & C++ only allow switches on integer types, since it was pointless with other types." My opinion, and it's only that, is that as soon as you start switching on non-primitives you need to start thinking about "equals" versus "==". Firstly comparing two strings can be a fairly lengthy procedure, adding to the performance problems that are mentioned above. Secondly if there is switching on strings there will be demand for switching on strings ignoring case, switching on strings considering/ignoring locale,switching on strings based on regex.... I would approve of a decision that saved a lot of time for the language developers at the cost of a small amount of time for programmers. |
|||||||
|
Beside the above good arguments, I will add that lot of people today see I don't fully share this opinion, I think But indeed, it is worth looking at the case where you need a switch, and see if it cannot be replaced by something more OO. For example enums in Java 1.5+, perhaps HashTable or some other collection (sometime I regret we don't have (anonymous) functions as first class citizen, as in Lua — which doesn't have switch — or JavaScript) or even polymorphism. |
|||
|
Reminds me of this thread : http://groups.google.com/group/comp.lang.java.programmer/browse_thread/thread/ddee6c336894e00d |
|||||
|
Its a breeze in Groovy; I embed the groovy jar and create a groovy utility class to do all this thing and more which I find 'exasperating' to do in Java( since I am stuck using Java 6 in the enterprise.)
|
|||
|