JDK-1223179 : switch on strings
  • Type: Enhancement
  • Component: tools
  • Sub-Component: javac
  • Affected Version: 1.0
  • Priority: P3
  • Status: Closed
  • Resolution: Won't Fix
  • OS: solaris_2.4
  • CPU: sparc
  • Submitted: 1995-10-02
  • Updated: 1995-10-09
  • Resolved: 1995-10-09
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