Switch statement in Java

If provided value to our switch will match with the value of one of the cases then this case will be executed.

public class Main {
public static void main(String[] args) {
String str = "white";

switch(str) {
case "black":
System.out.println("#000000");
break;
case "white":
System.out.println("#FFFFFF");
break;
case "silver":
System.out.println("#C0C0C0");
break;
default:
System.out.println("default color");
System.out.println("#000000");
}
}
}

The above program will print #FFFFFF because the value of String str is white and the second case has value white so they are matching and this case will get executed. The switch statement cannot have two cases with similar values, it will not just compile.

public class Main {
public static void main(String[] args) {
String str = "white";

switch(str) {
case "black":
System.out.println("#000000");
break;
case "white":
System.out.println("#FFFFFF");
case "silver":
System.out.println("#C0C0C0");
default:
System.out.println("default color");
System.out.println("#000000");
}
}
}

Exact same code but without the last two break keywords. The program will print

#FFFFFF
#C0C0C0
default color
#000000

break keyword stops the case execution. Without break, it will jump to the next case and so on. So it’s important to have it. Sometimes we can avoid breaks to meet multiple coditions. For example

public class Main {
public static void main(String[] args) {
String str = "apple";

switch(str) {
case "apple":
case "orange":
case "banana":
System.out.println("Fruit");
break;
case "cucumber":
case "tomato":
case "broccoli":
System.out.println("Vegetable");
break;
default:
System.out.println("I don't know");
}
}
}

Output from the above program is Fruit because the first case is a match and it will be executed. It’s an empty case and there is no break so it will go to the next case and there is no break as well and it will go to the next one. Here it will print out and there is a break so it will exit the switch statement.

The switch statement can work with specific data types only.

  • byte and Byte
  • short and Short
  • char and Character
  • int and Integer
  • String
  • enum values

The last thing I want to discuss is the default statement. Default statement executed when there is no match with cases. It’s optional and can appear anywhere within the switch statement.

public class Main {
public static void main(String[] args) {
int num = 7;

switch(num) {
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("Some number");
}
}
}

Output is Some number because there is no case with value 7 so the default case is executed.

This article is part of the series of articles to learn Java programming language from Tech Lead Academy:1. Introduction to programming 
2. OS, File, and File System
3. Working with terminal
4. Welcome to Java Programming Language
5. Variables and Primitives in Java
6. Methods with Java
7. Java Math Operators and special operators
8. Conditional branching in Java
9. Switch statement in Java
10. Ternary operator in Java
11. Enum in Java
12. String class and its methods in Java
13. Loops in Java
14. Access modifiers in Java
15. Static keyword in Java
16. The final keyword in Java
17. Class and Object in Java
18. Object Oriented Programming in Java
19. OOP: Encapsulation in Java
20. Inheritance in Java
21. Abstraction in Java
22. Polymorphism in Java
23. Overriding vs Overloading in Java
24. OOP Design Principles in Java
25. Array in Java
26. Data Structures with Java
27. Collection framework in Java
28. ArrayList in Java
29. Set in Java
30. Map in Java
31. LocalDate in Java
32. Exception in Java
33. IO in Java
34. Design Patterns
35. Generics in Java
36. Multithreading in java
37. JUnit
38. Big O Notation for coding interviews
39. Top 17 Java coding interview questions for SDET

Software Developer, Java Instructor https://www.techleadacademy.io/

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store