Why switch case statement in Java doesnt allow string as input parameter?


Switches based on integers can be optimized to very efficent code. Switches based on other data type can only be compiled to a series of if() statements. For that reason C & C++ only allow switches on integer types, since it was pointless with other types.”

Other opinion is as soon that as you start switching on non-primitives you need to start thinking about “equals” versus “==”.

Firstly comparing two strings can be a fairly lengthy procedure, adding to the performance problems that are mentioned above.

Secondly if there is switching on strings there will be demand for switching on strings ignoring case, switching on strings considering/ignoring locale,switching on strings based on regex

2 thoughts on “Why switch case statement in Java doesnt allow string as input parameter?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s