Java TutorialLesson 15Free preview
Java Switch
This lesson is part of Java · All levels
Summary
The switch is a branching alternative when checking one variable against many fixed values. Modern Java supports switch expression with arrows -> and default pieces that are no longer mandatory to use break. This approach makes code more concise and avoids unintended fall-throughs.
Key points
- The switch is suitable for comparing one variable with many constant values.
- Use arrows -> on the switch expression so you don't have to write break
- The default block runs when there is no case that matches
- The classic version uses cases, semicolons, and break to stop executions
Full lesson
Code examples and quizzes
Full lessons, code examples, and quizzes are available to subscribers. 5 questions.