Switch Statement in Java provides a way to select one option among multiple options.
In Java switch statement it provides multiple case statement based on switch expression the appropriate case statement is get executed
Syntax of switch statement
1 2 3 4 5 6 7 8 9 10 11 12 | switch (expression){ case label 1: statements; break; case label 2: statements; break; case label 3: statement; break; default: statement; |
The switch expression work with primitive byte short int, char and with its wrapper class, enum type and String Object( added from java7).
case label should also be one the above specified type and it should be constant or literal.
break statement is used to break the execution of switch statement. It transfer control after the switch statement.
If break is not used it execute all cases statement including default followed by currant case statement
The default statement is optional if expression does not match with any label then default case is executed.
Switch does not allows duplicate case value.
Only constants and literals are allowed in case value
Facts About Switch
- Switch Expression can have primitive byte short int, char and Its Wrapper classes, enum and String Objects.
- Case values also be one of the type allowed in switch expression.
- Case value allows only constant and literals.Variables are not allowed.
- Switch can have any number of case statements in any sequence.
- Duplicate case statement is not allowed.
- Default and Break statements is optional
- If any case expression does not match then default statement is executed.
Switch statement with integer
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | public class SwithExample { public static void main(String[] s) { int number = 3; switch (number) { case 1: System.out.println("Your lucky number is " + number); break; case 2: System.out.println("Your lucky number is " + number); break; case 3: System.out.println("Your lucky number is " + number); break; default: System.out.println("you did not choose your lucky number"); } } } |
Output
1 | Your lucky number is 3 |
Switch Statement with Byte Wrapper class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | public class SwitchByte { public static void main(String[] args) { Byte b1 = new Byte("10"); switch (b1) { case 5: System.out.println("Byte value is 5"); break; case 10: System.out.println("Byte value is 10"); break; case 15: System.out.println("Byte value is 15"); break; case 20: System.out.println("Byte value is 20"); break; default: System.out.println("Not matching any case"); } } } |
Result
1 | Byte value is 10<br> |
Switch statement character expression
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | public class SwitchChar { public static void main(String[] args) { char c = 'c'; switch (c) { case 'a': System.out.println("Executing case a"); break; case 'b': System.out.println("Executing case b"); break; case 'c': System.out.println("Executing case c"); break; default: System.out.println("Executing default"); } } } |
Result
1 | Executing case c |
String Object in Switch Statement
From Java 7 String object is added with switch statement
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | public class SwitchString { public static void main(String[] args) { String day = "Friday"; switch (day) { case "Monday": System.out.println("Today is Monday"); break; case "Tuesday": System.out.println("Today is Tuesday"); break; case "Wednesday": System.out.println("Today is Wednesday"); break; case "Thursday": System.out.println("Today is Thursday"); break; case "Friday": System.out.println("Today is Friday"); break; case "Saturday": System.out.println("Today is Saturday"); break; case "Sunday": System.out.println("Today is Sunday"); break; } } } |
Result
1 | Today is Friday |
Enum in Switch Statement
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | public class SwitchEnum { enum Color { RED, GREEN, BLUE, ORANGE, YELLOW }; public static void main(String[] args) { Color color = Color.RED; switch (color) { case RED: System.out.println("You choose RED color"); break; case GREEN: System.out.println("You choose GREEN color"); break; case BLUE: System.out.println("You choose BLUE color"); break; case ORANGE: System.out.println("You choose ORANGE color"); break; case YELLOW: System.out.println("You choose YELLOW color"); break; default: System.out.println("You color not in list"); } } } |
Result
1 | You choose RED color |
Java Switch program to print number of days in month
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | public class SwitchMonthDays { enum Month { JANUARY, FEBRUARY, MARCH, APRIL, MAY, JUNE, JULY, AUGUST, SEPTEMBER, OCTOBER, NOVEMBER, DECEMBER }; public static void main(String[] args) { Month month = Month.NOVEMBER; switch (month) { case JANUARY: case MARCH: case MAY: case JULY: case AUGUST: case OCTOBER: case DECEMBER: System.out.println("No of days =" + 31); break; case FEBRUARY: System.out.println("No of days may be 28 or 29"); break; case APRIL: case JUNE: case SEPTEMBER: case NOVEMBER: System.out.println("No of days =" + 30); break; default: System.out.println("Enter Correct Month Name"); } } } |
Output
1 | No of days =30 |
Java Program to check whether a character is consonant of vowel using switch statement.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | public class VowelandConsonantCheck { public static void main(String[] args) { char c = 'A'; switch (c) { case 'a': case 'e': case 'i': case 'o': case 'u': case 'A': case 'E': case 'I': case 'O': case 'U': System.out.println("Character " + c + " is Vowel"); break; default: System.out.println("Character " + c + " is Consonant"); break; } } } |
Output
1 | Character a is Vowel |