Why can't I use switch statement on a String?

前端 未结 16 2390
面向向阳花
面向向阳花 2020-11-21 07:57

Is this functionality going to be put into a later Java version?

Can someone explain why I can\'t do this, as in, the technical way Java\'s switch state

16条回答
  •  生来不讨喜
    2020-11-21 08:20

    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.

    The designers of C# decided that the style was important, even if there was no advantage.

    The designers of Java apparently thought like the designers of C.

提交回复
热议问题