Java Tutorial
Java Switch
switch picks a case from an integer, enum, or string. Remember break, or execution falls through.
Arrow cases
Modern Java uses -> cases. Each case runs its statement and does not fall through. That is the form used in these lessons.
Example
public class Main {
public static void main(String[] args) {
int day = 3;
switch (day) {
case 1 -> System.out.println("Mon");
case 2 -> System.out.println("Tue");
default -> System.out.println("later");
}
}
}Strings and enums
A switch can match a String or an enum constant, not just numbers. Use it when you have a small set of known labels — cleaner than a long if / else if chain that compares the same variable over and over.
Example
public class Main {
public static void main(String[] args) {
String light = "green";
switch (light) {
case "red" -> System.out.println("Stop");
case "green" -> System.out.println("Go");
default -> System.out.println("Slow down");
}
}
}The classic break form
The older case 1: ... break; form still works. Miss the break and execution "falls through" into the next case — a bug so common it is why the arrow form exists.
With colon-style cases, every branch needs its own break. Forget one and two cases run instead of one. The arrow form (case 1 -> ...) never falls through, so prefer it unless you truly want that behaviour.
Try It Yourself
Exercise: Read an int month (1–12) and print its season using arrow cases. (12, 1, 2 → Winter; 3–5 → Spring; 6–8 → Summer; 9–11 → Autumn.)
Show solution
switch (month) {
case 12, 1, 2 -> System.out.println("Winter");
case 3, 4, 5 -> System.out.println("Spring");
case 6, 7, 8 -> System.out.println("Summer");
default -> System.out.println("Autumn");
}Arrow cases can list several labels at once with commas — much tidier than a case per month.
Key Takeaways
- Use
switchfor a small set of known values; useiffor ranges and complex tests. - Arrow cases (
->) run one branch and never fall through. - Colon cases need a
breakeach, or execution falls into the next case. - A switch can match
int,String, andenumvalues.
Worked examples
The short listings above are there so you can see the grammar. The programs here use the same statements on quantities that already have units: a speed, a pH, a count of bases. They are classroom numbers. Air resistance is ignored. g is 9.81 m/s² unless a line says otherwise.
Open them in the Java editor at /java/try. Change one measurement and check whether the result still has the right unit.
Astronomy
Pick a spectral class
Stars are typed O, B, A, F, G, K, M. A switch maps a letter to a short reminder instead of a long if-else chain.
Example
public class Main {
public static void main(String[] args) {
char type = 'G';
switch (type) {
case 'O', 'B' -> System.out.println("hot");
case 'A', 'F' -> System.out.println("white");
case 'G' -> System.out.println("Sun-like");
default -> System.out.println("cool");
}
}
}Engineering
Menu for a logger
Field gear often offers a numeric menu. switch chooses the mode without nesting many ifs.
Example
public class Main {
public static void main(String[] args) {
int choice = 2;
switch (choice) {
case 1 -> System.out.println("calibrate");
case 2 -> System.out.println("log");
case 3 -> System.out.println("shutdown");
default -> System.out.println("?");
}
}
}