Is this functionality going to be put into a later Java version?
Can someone explain why I can’t do this, as in, the technical way Java’s switch statement works?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Switch statements with
Stringcases have been implemented in Java SE 7, at least 16 years after they were first requested. A clear reason for the delay was not provided, but it likely had to do with performance.Implementation in JDK 7
The feature has now been implemented in
javacwith a "de-sugaring" process; a clean, high-level syntax usingStringconstants incasedeclarations is expanded at compile-time into more complex code following a pattern. The resulting code uses JVM instructions that have always existed.A
switchwithStringcases is translated into two switches during compilation. The first maps each string to a unique integer—its position in the original switch. This is done by first switching on the hash code of the label. The corresponding case is anifstatement that tests string equality; if there are collisions on the hash, the test is a cascadingif-else-if. The second switch mirrors that in the original source code, but substitutes the case labels with their corresponding positions. This two-step process makes it easy to preserve the flow control of the original switch.Switches in the JVM
For more technical depth on
switch, you can refer to the JVM Specification, where the compilation of switch statements is described. In a nutshell, there are two different JVM instructions that can be used for a switch, depending on the sparsity of the constants used by the cases. Both depend on using integer constants for each case to execute efficiently.If the constants are dense, they are used as an index (after subtracting the lowest value) into a table of instruction pointers—the
tableswitchinstruction.If the constants are sparse, a binary search for the correct case is performed—the
lookupswitchinstruction.In de-sugaring a
switchonStringobjects, both instructions are likely to be used. Thelookupswitchis suitable for the first switch on hash codes to find the original position of the case. The resulting ordinal is a natural fit for atableswitch.Both instructions require the integer constants assigned to each case to be sorted at compile time. At runtime, while the
O(1)performance oftableswitchgenerally appears better than theO(log(n))performance oflookupswitch, it requires some analysis to determine whether the table is dense enough to justify the space–time tradeoff. Bill Venners wrote a great article that covers this in more detail, along with an under-the-hood look at other Java flow control instructions.Before JDK 7
Prior to JDK 7,
enumcould approximate aString-based switch. This uses the staticvalueOfmethod generated by the compiler on everyenumtype. For example: