Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

5
  • 1
    Nit pick: In fact string switch operates "as if it were using the String.equals method" (from the document you linked to). In reality, the compiler is free to optimize so that String.equals is not used ... provided that the net effect is the same. (And, I'd expect that it would use String.hashcode above a certain number of switch cases.) Commented Jul 14, 2013 at 5:32
  • True enough. Most compilers are allowed to do a ton of optimizations that don't change semantics though, so it's often redundant to point out little things like that; I had only specifically mentioned .equals() to be explicit that case isn't ignored. Nonetheless, I've updated the wording a bit. Commented Jul 14, 2013 at 5:39
  • String switches are not recommended practice as you're switch on an unbound domain. Switching on Enums is a typical middle ground here as you can have String like representation, but with semantic meaning. Oh and +1 for the complete answer BTW. Commented Jul 14, 2013 at 13:39
  • What are the advantages of using strings in a switch statement instead of integer constants? Commented Jul 16, 2013 at 5:16
  • 3
    the only business reason among those may be the security enhancements. Technical niceties are both debatable and utterly irrelevant for business people. Commented Jul 16, 2013 at 7:53