JCK feedback on "Strings in Switch" proposal
Ulf Zibis
Ulf.Zibis at gmx.de
Sun May 24 08:37:38 PDT 2009
Am 19.05.2009 14:42, Dmitry Miltsov schrieb:
> Hello,
>
> The JCK engineers have reviewed the "Strings in switch" proposal
> and found one issue to clarify.
>
> "14.11 The switch Statement" states:
> ------------------------------------------------------------------
> ...
> Otherwise, execution continues by comparing the value of the
> Expression with each case constant. Then there is a choice:
> * If one of the case constants is equal to the value of the
> expression, then we say that the case matches ...
> ------------------------------------------------------------------
>
> It would be good to clarify what "equal" means in case of the
> String values. It would be useful to mention that in this case
> the result of comparison will be "expression.equals(constant)",
> not "expression == constant" as for other types.
>
I think it would be better to define "expression == constant" for
several reasons.
(1) "switch..case" syntax historically is expected to be very fast. See
discussions on:
http://forums.java.net/jive/message.jspa?messageID=4146#4146
http://forums.java.net/jive/message.jspa?messageID=14216#14216
http://forums.java.net/jive/thread.jspa?threadID=504
(2) Comparison for identity would better match to legacy semantics of
"switch..case" statement.
(3) Don't obstruct later, more general extensions on "switch..case"
statement. See:
http://mail.openjdk.java.net/pipermail/coin-dev/2009-March/001182.html
http://forums.java.net/jive/message.jspa?messageID=4146#4146
There are 40 votes on Bug ID: 5012262
<http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5012262>
Please note, that these votes are for "Strings AND Objects in switch",
and not for just "Strings in switch".
Strings, which are only equal, could simply be made identical by
String#intern().
Example:
switch (myStringFromElsewere.intern()) {
case STRING1 : ...; // all constants are interned by definition
case STRING2 : ...; // " "
case "foo" : ...; // automatically interned
case "bar" : ...; // " "
...
}
-Ulf
More information about the coin-dev
mailing list