Home >>Java Tutorial >Java Switch Statement
To execute one statement from the multiple conditions, the Java switch statements are generally used. The Java switch statement generally permits a variable to be tested for the equality against a list of values that are present in the program. Each of these values in the statement is called as a case, and the variable that is generally being switched on is made sure to get checked for each of the case. In characteristics, the java switch statements are similar to the if-else-if ladder statement. The switch statements are generally known to works with String, byte, int, long, short, enum types, and some of the wrapper types such as Short, Byte, Long, and Int. Since Java 7, Programmers can use the strings in the switch statement in Java since the Java 7 is launched. In simple words, the equality of a variable is tested against the multiple values by the switch statements.
Here are some of the points that should be kept in mind while working with the Switch statements in Java language:
Here is the syntax of the switch statements in Java depicted below that will help you in understanding the functionality and the declaration of the switch statements in Java:
switch(expression) { case value1: //code that is to be executed; break; //optional case value2: //code that is to be executed; break; //optional ...... default: code that is to be executed, if all cases are not matched; }
Here are the examples of the switch statements in different contexts:
public class SwitchEx { public static void main(String[] args) { //Declaring a variable for switch expression int num=2; //Switch expression switch(num) { //Verify Case and print statement case 1: System.out.println("Today is monday"); break; case 2: System.out.println("Today is Tuesday"); break; case 3: System.out.println("Today is Wednesday"); break; //and so on till case 7 //Here is Default case statement default:System.out.println("Wrong Choice"); } } }
2. Here is another example that is basically the program to check Vowel or Consonant:
As a fact we know that if the characters are A, E, I, O, or U, then only it will be considered as a vowel otherwise it will be considered as a consonant. You can relax as it is not case-sensitive.
public class SwitchVowel { public static void main(String[] args) { char ch='E'; switch(ch) { case 'a': System.out.println("a is vowel"); break; case 'e': System.out.println("e is Vowel"); break; case 'i': System.out.println("i is Vowel"); break; case 'o': System.out.println("o is Vowel"); break; case 'u': System.out.println("u is Vowel"); break; case 'A': System.out.println("A is Vowel"); break; case 'E': System.out.println("E is Vowel"); break; case 'I': System.out.println("I is Vowel"); break; case 'O': System.out.println("O is Vowel"); break; case 'U': System.out.println("U is Vowel"); break; default: System.out.println("Given Character is Consonant"); } } }
The Java Switch Statement are known as a fall-through, in layman’s terms it simply executes all the statements just after the first match in case, a break statement is not present there.
Here is an example of that that will clear your concept about the Java Switch Statement of being a fall-through:
//Java Switch Example without break public class SwitchEx{ public static void main(String[] args) { int num=5; switch(number) { //Here is the switch cases without break statements case 5: System.out.println("5"); case 10: System.out.println("10"); case 15: System.out.println("15"); default: System.out.println("its not 5,10,15"); } } }
Java language generally allows the programmers to use the strings in switch expression since the Java SE 7 version was released. Please note that the case statement must be string literal.
Here is an example of that that will clear your concept about the Java Switch Statement being with a string:
//Java Program to demonstrate the use of Java Switch //statement with String public class SwitchString{ public static void main(String[] args) { //Declaring String variable String levelStr="Fresher"; int lev=0; switch(levelStr) { case "Fresher": lev=1; break; case "Intermediate": lev=2; break; case "Experts": lev=3; break; default: level=0; break; } System.out.println("You are : "+level); } }
The programmers can generally use the switch statement inside the other switch statement in the Java language and is also known as the nested switch statement.
Here is an example of that that will clear your concept about the Nested Switch Statement:
public static void main(String[] args) { switch(sandwich){ case: "Chicken": System.out.println("This is Chicken Sandwich"); break; case: "Paneer": System.out.println("This is Paneer Sandwich"); break; case "Veg": switch(fill-type) { case: "Tomato": System.out.println("This is Tomato filling Sandwich"); break; case: "Eggplant": System.out.println("THis is Eggplant filling Sandwich"); break; case: "Swiss cheese": System.out.println("This is Swiss filling Sandwich"); break; default: System.out.println("There is no any Choice"); } default: System.out.println("Sandwich Choice was not Chicken,Paneer or Veg"); } }
In Java language the use of four wrapper classes that are Short, Byte, Integer and Long in the switch statement is permitted to the programmers.
Here is an example of the Wrapper in Switch Statement:
public class WrapperSwitch{ public static void main(String args[]) { Integer ag =62; switch (age) { case (12): System.out.println("You are underage"); break; case (18): System.out.println("Congrates You are eligible"); break; case (62): System.out.println("senior citizen."); break; default: System.out.println("Invalid valid age."); break; } } }