switch as an Expression
Return values from switch.
switch as an Expression is a free Java Academy lesson on CoddyKit — lesson 2 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.
Statements vs Expressions
A statement performs an action. An expression produces a value. Modern Java lets switch be an expression, so you can assign its result directly to a variable.
Assigning a switch Result
When switch appears on the right side of =, each branch supplies the value. The arrow form returns the value after -> automatically.
public class Main {
public static void main(String[] args) {
int day = 6;
String type = switch (day) {
case 6, 7 -> "Weekend";
default -> "Weekday";
};
System.out.println(type);
}
}Semicolon After the Block
Because a switch expression is part of a statement, it ends with a semicolon after the closing brace. Forgetting it is a common compile error.
public class Main {
public static void main(String[] args) {
int n = 2;
int doubled = switch (n) {
case 1 -> 2;
case 2 -> 4;
default -> 0;
};
System.out.println(doubled);
}
}Exhaustiveness
A switch expression must cover every possible value. For most types this means including a default branch, otherwise the code will not compile.
public class Main {
public static void main(String[] args) {
int score = 85;
char grade = switch (score / 10) {
case 10, 9 -> 'A';
case 8 -> 'B';
case 7 -> 'C';
default -> 'F';
};
System.out.println(grade);
}
}Enums Need No default
When you switch over an enum and cover every constant, the compiler knows it is exhaustive. A default branch becomes optional.
public class Main {
enum Dir { NORTH, SOUTH, EAST, WEST }
public static void main(String[] args) {
Dir d = Dir.EAST;
String label = switch (d) {
case NORTH -> "Up";
case SOUTH -> "Down";
case EAST -> "Right";
case WEST -> "Left";
};
System.out.println(label);
}
}Returning Directly
A method can return a switch expression directly, making concise lookup methods.
public class Main {
static int sides(String shape) {
return switch (shape) {
case "triangle" -> 3;
case "square" -> 4;
case "pentagon" -> 5;
default -> 0;
};
}
public static void main(String[] args) {
System.out.println(sides("square"));
}
}Numeric Results
Branches can return numbers, perform arithmetic, or call methods. Every branch must produce a value of the same type as the variable being assigned.
public class Main {
public static void main(String[] args) {
int month = 2;
int days = switch (month) {
case 2 -> 28;
case 4, 6, 9, 11 -> 30;
default -> 31;
};
System.out.println("Days: " + days);
}
}Using the Result Inline
The value from a switch expression can be used immediately, for example inside a println call.
public class Main {
public static void main(String[] args) {
int code = 1;
System.out.println("Status: " + switch (code) {
case 0 -> "OK";
case 1 -> "Warning";
default -> "Error";
});
}
}Type Consistency
All branches must agree on the result type. If one branch returns an int and another a String, the code will not compile. Keep branches consistent.
public class Main {
public static void main(String[] args) {
int n = 3;
double factor = switch (n) {
case 1 -> 0.5;
case 2 -> 1.0;
default -> 1.5;
};
System.out.println(factor);
}
}Replacing Ternary Chains
A switch expression often reads more clearly than nested ternary operators when mapping one value to many results.
public class Main {
public static void main(String[] args) {
int rating = 4;
String stars = switch (rating) {
case 1 -> "*";
case 2 -> "**";
case 3 -> "***";
case 4 -> "****";
default -> "*****";
};
System.out.println(stars);
}
}Combining With Variables
You can use the result of a switch expression in further calculations, just like any other value.
public class Main {
public static void main(String[] args) {
int tier = 2;
int discount = switch (tier) {
case 1 -> 5;
case 2 -> 10;
default -> 0;
};
int price = 100 - discount;
System.out.println("Price: " + price);
}
}Quick Check
Why must a switch expression usually include a default branch?
Recap
You learned to use switch as an expression:
- It produces a value you can assign or return
- End the assignment with a semicolon
- It must be exhaustive (usually needs
default) - Enums covering all constants need no
default - All branches must share the same result type
Frequently asked questions
Is the “switch as an Expression” lesson free?
Yes — the full text of “switch as an Expression” 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 “switch as an Expression”?
Return values from switch. 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 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “switch as an Expression” 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