Java Notes
Complete guide to Java switch statement — traditional switch, enhanced switch expressions (Java 14+), fall-through behavior, pattern matching, and when to use switch vs if-else.
The switch statement provides an elegant way to select among multiple alternatives based on a single value. Java has significantly enhanced switch in recent versions, making it more powerful and less error-prone.
Traditional Switch Statement
Day 3 = Wednesday
Switch with Strings (Java 7+)
Starting the application...
Fall-Through Behavior
Without break, execution falls through to the next case:
Month 8 → Summer === Without break (bug!) === One Two Three
Enhanced Switch Expressions (Java 14+)
Modern Java provides arrow syntax that eliminates fall-through bugs:
Day 5 is a Weekday Friday - Weekend is coming! Days in month 4: 30
Switch with Enums
Priority: HIGH → Handle today
Allowed Types in Switch
| Type | Since | Example |
|---|---|---|
byte, short, int, char | Java 1.0 | switch (intVar) |
Wrapper: Byte, Short, Integer, Character | Java 5 | switch (integerObj) |
enum | Java 5 | switch (direction) |
String | Java 7 | switch (command) |
| Pattern matching | Java 21 | switch (obj) |
NOT allowed: long, float, double, boolean
Pattern Matching in Switch (Java 21+)
Long string: Hello, World!
Practical Example: Simple Calculator
Enter expression (e.g., 5 + 3): 10 / 3 10.00 / 3.00 = 3.33
Common Mistakes
- Forgetting break in traditional switch — causes unintentional fall-through. Use arrow syntax (Java 14+) to avoid this entirely.
- Not handling null Strings —
switch(null)throws NullPointerException. Check for null before the switch. - Using unsupported types —
long,float,double,booleancannot be used in switch. Use if-else for these. - Duplicate case values — compile error. Each case value must be unique.
- Non-constant case labels — case values must be compile-time constants (literals, final variables, enum values).
Interview Questions
Q1: What is the difference between switch statement and switch expression?
Answer: A switch statement executes code blocks (uses break). A switch expression (Java 14+) returns a value (uses -> or yield). Expressions are exhaustive (must cover all cases), don't have fall-through, and can be used anywhere a value is expected.
Q2: What is fall-through in switch and how to prevent it?
Answer: Fall-through occurs when a case lacks a break statement — execution continues into the next case. Prevent it by: (1) always including break in traditional switch, (2) using enhanced switch with arrow syntax (->) which doesn't fall through.
Q3: Can you use switch with String in Java?
Answer: Yes, since Java 7. The switch statement uses String.equals() internally for comparison (case-sensitive). Always check for null before the switch to avoid NullPointerException.
Q4: When should you use switch vs if-else?
Answer: Use switch when comparing a single variable against multiple constant values. Use if-else for ranges, complex conditions, multiple variables, or types not supported by switch. Switch is generally more readable and may be optimized by the compiler (lookup/table jump).
Q5: What is the yield keyword in switch expressions?
Answer: yield (Java 14+) is used to return a value from a multi-line block in a switch expression. It's needed when the case body has multiple statements: case X -> { stmt1; stmt2; yield value; }.
Summary
Java's switch statement selects among multiple alternatives based on a single value. The modern switch expression (Java 14+) eliminates fall-through bugs, returns values directly, and supports pattern matching (Java 21). Use switch for clean, readable multi-way branching; prefer arrow syntax for safety; and remember that switch only works with specific types (int, char, String, enum, and patterns).
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Switch-Case in Java.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Java Master Course topic.
Search Terms
java-master-course, java master course, java, master, course, fundamentals, control, statements
Related Java Master Course Topics