Arrow-Style switch
Use the modern switch arrow syntax.
Arrow-Style switch is a free Java Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Java Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
The Old switch Problem
The classic Java switch statement used colon labels and required break after every case. Forgetting break caused fall-through bugs where execution slid into the next case.
Modern Java (14+) introduces the arrow syntax that fixes this entirely.
Arrow Syntax Basics
With arrow-style switch, each label uses -> instead of :. There is no fall-through and no break needed. Each branch runs only its own code.
public class Main {
public static void main(String[] args) {
int day = 3;
switch (day) {
case 1 -> System.out.println("Monday");
case 3 -> System.out.println("Wednesday");
default -> System.out.println("Other day");
}
}
}No More break
In the old style you had to write break; after each case. The arrow form removes that burden completely. Only the matching branch executes.
public class Main {
public static void main(String[] args) {
char grade = 'B';
switch (grade) {
case 'A' -> System.out.println("Excellent");
case 'B' -> System.out.println("Good");
case 'C' -> System.out.println("Average");
default -> System.out.println("Needs work");
}
}
}Block Bodies
When a branch needs more than one statement, wrap it in braces { }. The block can contain any number of statements.
public class Main {
public static void main(String[] args) {
int code = 2;
switch (code) {
case 2 -> {
System.out.println("Processing code 2");
System.out.println("Done");
}
default -> System.out.println("Unknown");
}
}
}Switching on Strings
Arrow switch works on String values just like the old style. Comparisons use equals internally, so they are case-sensitive.
public class Main {
public static void main(String[] args) {
String fruit = "apple";
switch (fruit) {
case "apple" -> System.out.println("Red or green");
case "banana" -> System.out.println("Yellow");
default -> System.out.println("Unknown fruit");
}
}
}The default Branch
The default branch handles any value not covered by other cases. It is good practice to always include one so unexpected input is handled gracefully.
public class Main {
public static void main(String[] args) {
int level = 99;
switch (level) {
case 1 -> System.out.println("Beginner");
case 2 -> System.out.println("Intermediate");
default -> System.out.println("Unknown level: " + level);
}
}
}Switching on Enums
Arrow switch pairs perfectly with enum types. You can omit the enum name prefix on each label, keeping branches short and clean.
public class Main {
enum Color { RED, GREEN, BLUE }
public static void main(String[] args) {
Color c = Color.GREEN;
switch (c) {
case RED -> System.out.println("Stop");
case GREEN -> System.out.println("Go");
case BLUE -> System.out.println("Calm");
}
}
}Calling Methods in Branches
Each arrow branch can call any method. This keeps the switch readable while delegating real work elsewhere.
public class Main {
static void greet(String who) {
System.out.println("Hello, " + who);
}
public static void main(String[] args) {
int who = 1;
switch (who) {
case 1 -> greet("Alice");
case 2 -> greet("Bob");
default -> greet("Stranger");
}
}
}Cleaner Than if-else
Arrow switch often replaces long if-else chains. When you compare one variable against many fixed values, switch reads more clearly.
public class Main {
public static void main(String[] args) {
int month = 4;
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");
}
}
}Side Effects Only
A switch statement (arrow form used here) performs actions but does not produce a value. To return a value, you use a switch expression, covered in the next lesson.
public class Main {
public static void main(String[] args) {
int n = 5;
switch (n % 2) {
case 0 -> System.out.println(n + " is even");
case 1 -> System.out.println(n + " is odd");
}
}
}Multiple Values, One Branch
Several values can share a branch by separating them with commas. This is a preview of multi-label cases covered later.
public class Main {
public static void main(String[] args) {
char c = 'e';
switch (c) {
case 'a', 'e', 'i', 'o', 'u' -> System.out.println("Vowel");
default -> System.out.println("Consonant");
}
}
}Quick Check
What is the main advantage of arrow-style switch over the old colon style?
Recap
You learned the modern arrow switch syntax:
- Use
->instead of: - No fall-through and no
breakneeded - Use braces
{ }for multi-statement branches - Comma-separate labels to share a branch
- Always include a
defaultbranch
Frequently asked questions
Is the “Arrow-Style switch” lesson free?
Yes — the full text of “Arrow-Style switch” is free to read here on the web, and the Java Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Java Academy course, upgrade to CoddyKit PRO.
What will I learn in “Arrow-Style switch”?
Use the modern switch arrow syntax. You practise Java Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Java Academy?
No prior experience is required. Java Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Arrow-Style switch” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Java Academy lesson?
Yes. Every Java Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Arrow-Style switch
- switch as an Expression
- The yield Keyword
- Multi-Label Cases