Java switch statement

A Java switch selects a case based on one selector value. In a traditional colon-style switch, use break when execution should stop after the matching case.

A simple example

Match one case

  1. Start with Value 2
  2. Do this Match case 2
  3. Result two
The matching case supplies the word. A different value can take the default path.
Main.java
class Main {
    public static void main(String[] args) throws Exception {
        int n = 2;
        switch (n) {
            case 2 -> System.out.println("two");
            default -> System.out.println("other");
        }
    }
}
Output
two
Queued or retry: Pending. Sent: Done. Other status: Review.
Route a parcel update

A parcel dashboard receives status updates. A queued parcel and a parcel awaiting retry both belong in the pending tray. Mark a sent parcel done and send unfamiliar statuses for review.