United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
Bug ID: JDK-1223179 switch on strings
JDK-1223179 : switch on strings

Details
Type:
Enhancement
Submit Date:
1995-10-02
Status:
Closed
Updated Date:
1995-10-09
Project Name:
JDK
Resolved Date:
1995-10-09
Component:
tools
OS:
solaris_2.4
Sub-Component:
javac
CPU:
sparc
Priority:
P3
Resolution:
Won't Fix
Affected Versions:
1.0
Fixed Versions:

Related Reports

Sub Tasks

Description
From daemon@java Sun Oct  1 18:18 PDT 1995
To: java-interest@java
Subject: switch is too limiting


So I've discovered, through the time-honored debugging method known as the
compiler error, that switch statements only work if the test and the constant
are both ints or can be cast to ints (i.e. numerics or chars).

sigh.

I would have hoped that switch in java would be more flexible than
that, and that you could switch with strings or other objects.  Yes,
I know that switch would only work if == was meaningful for object
values, and that gets into the whole operator overloading discussion,
so I won't start that.

But it does seem awfully limiting to me.  I would love to be able to
use strings as identifiers, for example, and to be able to do something
like this:

switch (alignment) {
    case "left" : {
      // code to align stuff left
    }
    case "right" : {
      // code to align stuff right
    }
    case "center" : {
      // code to align stuff center
    }
}

Instead, I either have to use a whole lot of nested ifs (in which I can
do alignment.equals("right")), or set up integer constants to represent
everything I wanted to be a string in the first place.

sigh.

Maybe in 2.0?


Laura

                                    

Comments
EVALUATION

Don't hold your breath.  

Nothing resembling this is in our plans.

                                     
2004-06-11



Hardware and Software, Engineered to Work Together