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 conditions. 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.
Please take my Java Course for video lectures.This article is part of the series of articles to learn Java programming language from Tech Lead Academy:Introduction to programming
OS, File, and File System
Working with terminal
Welcome to Java Programming Language
Variables and Primitives in Java
Convert String to numeric data type
Input from the terminal in Java
Methods with Java
Java Math Operators and special operators
Conditional branching in Java
Switch statement in Java
Ternary operator in Java
Enum in Java
String class and its methods in Java
Loops in Java
Access modifiers in Java
Static keyword in Java
The final keyword in Java
Class and Object in Java
Object-Oriented Programming in Java
OOP: Encapsulation in Java
OOP: Inheritance in Java
OOP: Abstraction in Java
OOP: Polymorphism in Java
The method Overriding vs Overloading in Java
Array in Java
Data Structures with Java
Collection framework in Java
ArrayList in Java
Set in Java
Map in Java
Date and Time in Java
Exception in Java
How to work with files in Java
Design Patterns
Generics in Java
Multithreading in java
Annotations in Java
Reflection in Java
Reflection & Annotations - The Powerful Combination
Run terminal commands from Java
Lambda in Java
Unit Testing in Java
Big O Notation for coding interviews
Top Java coding interview questions for SDET