Using two values for one switch case statement
⚡TLDR
Invoke case fall-through in scenarios where multiple case values yield the same outcome. Use break
statements to avoid unintentional fall through and achieve a unified response. A concise example is given below:
This code outputs "Number is 1 or 2" for both cases, hence managing two values with a uniform reply.
Taming the switch statement
Post Java SE 7 upgrade, String objects can be employed within switch expressions. This minor but significant tweak offers a more expressive and refined syntax:
Linked
Linked
Was this article helpful?