Why do we need break after case statements?

后端 未结 17 2034
猫巷女王i
猫巷女王i 2020-11-22 04:34

Why doesn\'t the compiler automatically put break statements after each code block in the switch? Is it for historical reasons? When would you want multiple code blocks to e

17条回答
  •  无人及你
    2020-11-22 05:04

    Java is derived from C, whose heritage includes a technique known as Duff's Device . It's an optimization that relies on the fact that control falls through from one case to the next, in the absence of a break; statement. By the time C was standardized, there was plenty of code like that "in the wild", and it would have been counterproductive to change the language to break such constructions.

提交回复
热议问题