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 switch for a small set of known values; use if for ranges and complex tests.
  • Arrow cases (->) run one branch and never fall through.
  • Colon cases need a break each, or execution falls into the next case.
  • A switch can match int, String, and enum values.

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("?");
    }
  }
}

FAQ: Java Switch

Common questions about this page.

What is the StudyGrid Java tutorial?

The StudyGrid Java tutorial is a full beginner-to-advanced track: syntax, types, input, loops, methods, classes, collections, generics, maps, and lambdas. Each chapter has copy-and-run examples.

Should I run java switch examples locally for better learning?

Yes. Use the browser editor on StudyGrid for a quick check, then Download the example and run it on your computer. Local runs show real errors and the real toolchain, which is one of the fastest ways to learn java switch in this Java Java lesson (Java Switch).

Is the Java editor the same as Try Python or Try C++?

No. Try Java compiles with javac at /java/try and shows stdout plus compiler messages. Try Python stays at /try. Try C++ stays at /cpp/try. Java lessons never open those editors.

Do I need to install a JDK to learn Java?

No. Open a chapter, click Try it in Java, and compile in the browser. You can also download a .java file and compile locally with javac.

Where should I start the Java tutorial?

Start at Java Intro, then Get Started and Syntax. After the first program, continue to output, variables, and if-else. After classes, open Java Examples, then generics, map, and lambdas. Use Next at the bottom of each chapter.

Is the Java tutorial free?

Yes. The Java workshop on StudyGrid (studygrid.in) is free: dashboard, chapters, and the compile-and-run editor.