Java switch
Store the whole number 2 under the name choice. Build a switch that lists case 1, case 2, and case 3, each with its own println, plus a default. When you compile and run, the screen should show mid — one line — because case 2 matches. That is what this page teaches: one value, several fixed labels, one chosen path.
One value, several fixed labels
Create a small program file named Dial.java. The public class on the first line must match the file name: public class Dial. Do not leave an older Gate or Mint class name in this file; the compiler expects the class name and the file name to agree.
Inside main, store a whole number before the switch:
int choice = 2;
That line does not print. It only gives the name choice the value 2. choice starts with a small c. There are no quotes around 2. The semicolon ends the storing line. After that comes the switch itself:
switch (choice) {
case 1:
System.out.println("low");
break;
case 2:
System.out.println("mid");
break;
case 3:
System.out.println("high");
break;
default:
System.out.println("none");
break;
}
The word switch is lowercase. Inside the parentheses sits the name choice — the value Java will compare to each case label. Each case line ends with a colon, not a semicolon. Under case 2 sits System.out.println("mid"); then the word break and a semicolon. When choice holds 2, Java enters case 2, prints mid, hits break, and leaves the whole switch. case 3 and default do not run on this turn. Save the file before you compile.
Program that prints mid
This is the version you compile first. choice is 2, so the matching label is case 2.
public class Dial {
public static void main(String[] args) {
int choice = 2;
switch (choice) {
case 1:
System.out.println("low");
break;
case 2:
System.out.println("mid");
break;
case 3:
System.out.println("high");
break;
default:
System.out.println("none");
break;
}
}
}
From the folder that holds Dial.java, run javac Dial.java. When the braces, parentheses, and colons match, javac writes no line of its own. Then run java Dial — not java Dial.java. Leave the .java ending off the second command. The same two commands work on Windows and on macOS; this page does not need a different shell spelling for either system. The program should print mid and stop. It should not ask you to type. If the terminal waits for keyboard input, a nextLine call from an older exercise is still in the file. Delete that call and keep the int plus the switch above. A wrong screen line does not mean you need a new JDK install.
Why mid and not low or high? Because the value in parentheses was 2, and case 2 is the label that matches. Java does not print the digit 2 on this run unless you write a println for that digit. It prints whatever println runs under the matched case. Parentheses hold the value being compared. Each case label is a fixed whole number here. break ends the switch so later cases stay quiet. default is waiting for values that match none of the cases — it stays unused while choice is 2.
When no case matches
Change only the stored value:
int choice = 9;
Leave the switch structure as it is. Now none of case 1, case 2, or case 3 matches. Java moves to default and runs System.out.println("none");. The screen should show none, one line.
public class Dial {
public static void main(String[] args) {
int choice = 9;
switch (choice) {
case 1:
System.out.println("low");
break;
case 2:
System.out.println("mid");
break;
case 3:
System.out.println("high");
break;
default:
System.out.println("none");
break;
}
}
}
Compile and run again: javac Dial.java, then java Dial. You should see none. You did not rewrite the case lines — only the value under choice changed. After you have seen none, you can put int choice = 2; back to return to the first demo. Both versions are valid Java. They teach the same switch shape with a matching case and with default.
Read the default run once more in order. choice holds 9. switch (choice) begins. case 1 does not match. case 2 does not match. case 3 does not match. Control continues at default. println("none") executes. One line on the screen. The digit 9 itself never appears unless you print it on purpose.
break stops later cases
break is not decoration. Without it, Java can keep running the next case’s lines after a match. That fall-through surprises beginners. Here is a short demo that forgets break on purpose after case 2 — keep it in a separate try, or read it carefully before you compile it as your main file:
public class Dial {
public static void main(String[] args) {
int choice = 2;
switch (choice) {
case 1:
System.out.println("low");
break;
case 2:
System.out.println("mid");
case 3:
System.out.println("high");
break;
default:
System.out.println("none");
break;
}
}
}
With choice equal to 2 and no break under case 2, the screen can show mid on one line and high on the next. case 2 matched, printed mid, then fell into case 3 and printed high. The first teaching demos keep break under every case so each path writes exactly one line. Put break back before you treat Dial.java as the kept lesson file again.
Rules that keep the labels clear
Parentheses around the switched value are required. Writing switch choice { … } without parentheses makes javac fail. Each case label on this page is a whole-number constant: case 1:, case 2:, case 3:. A colon follows the label. Writing case 2; with a semicolon instead of a colon is not the shape this lesson compiles. default has no value after it — just default: then the lines for “nothing matched.”
switch is not the same tool as if. if (ready) asks a true-or-false question. switch (choice) matches one value against a list of fixed labels. Do not write switch (ready) with a boolean and expect the same lesson shape as Gate.java. Keep this page’s demos on an int named choice and whole-number case labels. Comparisons such as if (score > 10) still belong to if when you need a range; switch shines when the values are discrete and known in advance.
A common mix-up is printing the syntax itself: System.out.println("switch (choice)");. That writes the characters switch (choice). It does not pick a case. Another mix-up is leaving an if / else Gate demo in the same main and wondering why the screen shows pass instead of mid. This session’s main should hold the int and the switch only.
📊 choice and the screen
| choice | Screen |
|---|---|
| 1 | low |
| 2 | mid |
| 3 | high |
| 9 | none |
Use the table as a check after a run, not instead of one. choice 2 with the first demo prints mid. choice 9 with the second demo prints none. choice 1 prints low. choice 3 prints high. If mid and high both appear for choice 2, a break is missing under case 2 — fix the breaks rather than reinstalling Java.
What you can do now
You can store a whole number, put it in switch (…), and give each fixed value its own case with a println and a break. When a case matches, that line runs; when nothing matches, default runs. The next page is for, where the same block of lines can run again and again while a counter changes.
🧠 Test Your Knowledge
Test Your Knowledge
Challenge yourself with this interactive quiz and see how well you understand the topic
📝 Instructions
- Read each question carefully
- Select the best answer for each question
- You can retake the quiz as many times as you want
- Your progress will be shown at the top