Today I explored advanced and tricky scenarios in Java Switch Case.
At first glance, switch-case looks simpleβ¦ but when you remove break, use default, or mix data types β things get very tricky π²
This is also a favorite topic in interviews.
πΉ Before Java 8 β Traditional Switch
int x = 2;
switch(x) {
case 1:
System.out.println("One");
break;
case 2:
System.out.println("Two");
break;
case 3:
System.out.println("Three");
break;
default:
System.out.println("Default");
}
π€ Output
Two
β break stops execution
β Only matching case executes
πΉ After Java 8+ (Arrow Syntax)
int x = 2;
switch(x) {
case 1 -> System.out.println("Hi");
case 2 -> System.out.println("Welcome");
default -> System.out.println("Hello");
}
π€ Output
Welcome
β No need for break
β No fall-through
πΉ Switch Expression (Java 12+)
int x = 2;
int result = switch(x) {
case 1 -> 10;
case 2 -> 20;
default -> 0;
};
System.out.println(result);
π€ Output
20
πΉ Java 17+ Pattern Matching
Object obj = "Hello";
switch(obj) {
case String s -> System.out.println(s.length());
case Integer i -> System.out.println(i * 2);
default -> System.out.println("Other");
}
π€ Output
5
π₯ Tricky Questions
π 1. Missing break (Fall-through)
int x = 2;
switch(x) {
case 1:
System.out.println("One");
case 2:
System.out.println("Two");
case 3:
System.out.println("Three");
default:
System.out.println("Default");
}
π€ Output
Two
Three
Default
β
Explanation
β Starts from matching case β case 2
β No break β continues executing all below
π 2. char vs ASCII
char ch = 'A';
switch(ch) {
case 65:
System.out.println("ASCII 65");
break;
case 'A':
System.out.println("Character A");
}
π€ Output
ASCII 65
β Explanation
β 'A' = 65 internally
β First matching case executes
π 3. Expression inside switch
int x = 10;
switch(x / 2) {
case 5:
System.out.println("Five");
break;
case 10:
System.out.println("Ten");
}
π€ Output
Five
β Explanation
β x/2 = 5 β matches case 5
π 4. Multiple case labels
int x = 2;
switch(x) {
case 1:
case 2:
System.out.println("One or Two");
break;
case 3:
System.out.println("Three");
}
π€ Output
One or Two
β Explanation
β Multiple cases share same block
π 5. Missing break again
int x = 2;
switch(x) {
case 1:
case 2:
System.out.println("One or Two");
case 3:
System.out.println("Three");
}
π€ Output
One or Two
Three
β Explanation
β Fall-through again due to missing break
π 6. Fall-through chain
int x = 3;
switch(x) {
case 1:
System.out.println("One");
break;
case 2:
System.out.println("Two");
case 3:
System.out.println("Three");
case 4:
System.out.println("Four");
break;
}
π€ Output
Three
Four
β Explanation
β Starts at case 3
β No break β continues
π 7. Default in middle
int x = 2;
switch(x) {
default:
System.out.println("Default");
case 1:
System.out.println("One");
break;
case 2:
System.out.println("Two");
}
π€ Output
Two
β Explanation
β Matching case runs directly
β Default is ignored
π 8. Default fall-through
int x = 5;
switch(x) {
case 1:
System.out.println("One");
default:
System.out.println("Default");
case 5:
System.out.println("Five");
}
π€ Output
Default
Five
β Explanation
β No match β default executes
β Then continues to case 5
π 9. final variable in case
final int a = 1;
int b = 2;
switch(b) {
case a:
System.out.println("A");
break;
case a + 1:
System.out.println("A+1");
}
π€ Output
A+1
β Explanation
β final makes it constant β allowed in case
π 10. String fall-through
String s = "Java";
switch(s) {
case "Java":
System.out.println("Java");
case "JAVA":
System.out.println("JAVA");
break;
}
π€ Output
Java
JAVA
β Explanation
β Missing break β fall-through
π 11. null in switch
String s = null;
switch(s) {
case "Hi":
System.out.println("Hi");
break;
default:
System.out.println("Default");
}
π€ Result
NullPointerException
β Explanation
β switch does NOT accept null
π 12. Duplicate case
int x = 1;
switch(x) {
case 1:
System.out.println("One");
break;
case 1:
System.out.println("Duplicate");
}
π€ Result
Compile Time Error
β Explanation
β Duplicate case not allowed
π 13. Duplicate constant
final int a = 2;
switch(2) {
case a:
System.out.println("A");
break;
case 2:
System.out.println("Two");
}
π€ Result
Compile Time Error
β Explanation
β Both represent same value β duplicate
π 14. Nested switch
int x = 1;
switch(x) {
case 1:
switch(x + 1) {
case 2:
System.out.println("Inner Two");
}
case 2:
System.out.println("Outer Two");
}
π€ Output
Inner Two
Outer Two
β Explanation
β Inner switch executes
β No break β outer continues
π§ Key Takeaways
β Missing break = fall-through
β default can be anywhere
β final variables allowed in case
β Duplicate case = compile error
β null not allowed
β Arrow syntax avoids fall-through
π Conclusion
Today I learned how small mistakes in switch-case can lead to unexpected outputs.
These tricky questions are extremely helpful for:
β Java interviews
β Debugging code
β Writing clean logic in automation
Step by step, Iβm strengthening my Java fundamentals for Selenium Automation π
π€ A Small Note
I used ChatGPT to help structure and refine this blog while ensuring the concepts remain aligned with my trainerβs explanations.

Top comments (0)