JDK-4811993 : RFE: Expand allowable case labels in switch statement
  • Type: Enhancement
  • Component: specification
  • Sub-Component: language
  • Affected Version: 1.4.0
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_98
  • CPU: x86
  • Submitted: 2003-02-03
  • Updated: 2007-01-12
  • Resolved: 2007-01-12
Related Reports
Duplicate :  
Relates :  
Relates :  
Name: rmT116609			Date: 02/03/2003

The Java 'switch' statement should be made into less of a special-case implementation oriented construct and more into a general-purpose multi-way branch construct that isn't tied down by its implementation.  The very basic
changes to get there include:

- Drop the requirements that case labels be constants. Allow them to be arbitrary expressions, and specify that cases are evaluated in textual order in case the expressions have side effects.

- Extend the types allowable in case labels to include long, and to include reference types.  Reference typed cases would be evaluated for a match under these rules:

If the variable and case label value are both null, then a match occurs.  If only one of the two is null, then no match occurs.  If neither is null and var.equals(lab) is true (where var is the variable and lab is the label
expression) then a match occurs.  Else, no match occurs.

This proposal would allow use of cases in plenty of situations where if/else chains are currently required.

No Java VM changes are proposed in this RFE.  The change is to the language-level switch construct, NOT the bytecode tableswitch and lookupswitch instructions.  Those instructions could still be used to implement certain
switch statements that are expressable in terms of their behavior.

Chained if/else statements can be used to provide the same control flow, but at a reduced level of readability.
(Review ID: 163356) 

EVALUATION Maybe; see the master request at 5012262.