Java switch case with examples
- Details
- Written by Nam Ha Minh
- Last Updated on 29 March 2020   |   Print Email
This article helps you understand and use the switch case construct in Java with code examples.
The switch-case construct is a flow control structure that tests value of a variable against a list of values. Syntax of this structure is as follows:
switch (expression) { case constant_1: // statement 1 break; case constant_2: // statement 2 break; case constant_3: // statement 3 break; //... case constant_n: // statement n break; default: // if all the cases do not match }
Some Rules about switch-case construct:
- The expression is a variable or an expression which must be evaluated to one of the following types:
- Primitive numbers: byte, short, char and int
- Primitive wrappers: Byte, Short, Characterand Integer.
- Enumerated types (enum type).
- String literals (since Java 1.7)
- The expression is a variable or an expression which must be evaluated to one of the following types:
- The constant_1, constant_2, constant_3, …, constant_n must be a constant or literals of the allowed types.
- Each case is tested from top to bottom, until a case is matched and a break statement is found.
- If a case matches the expression, the statements block after the case clause are executed, until a break statement is reached.
It is not required that each case must have a corresponding break statement. If a matching case block does not have a break statement, the execution will fall through the next case block, until a first break statement is reached or end of switch statement is encountered.
The statements block after default will be executed if there is no matching case found.
Some Java switch-case examples:
The following example shows a switch statement that tests for an integer variable. It products the output: The number is Three
int number = 3; String text = ""; switch (number) { case 1: text = "One"; break; case 2: text = "Two"; break; case 3: text = "Three"; break; default: text = "Other number"; } System.out.println("The number is: " + text);
The following example shows a switch statement that tests for a String variable, without a default block. It will output: The distance from earth to Jupiter is: 4
String planet = "Jupiter"; long distanceFromEarth = 0; switch (planet) { case "Mars": distanceFromEarth = 3; break; case "Saturn": distanceFromEarth = 5; break; case "Jupiter": distanceFromEarth = 4; break; case "Venus": distanceFromEarth = 1; break; } System.out.println("The distance from earth to " + planet + " is: " + distanceFromEarth);
The following example shows a switch statement that tests for an enum type, Priority. It will output the result: Task priority: 3
Priority priority = Priority.HIGH; int taskPriority = 0; switch (priority) { case LOW: taskPriority = 1; break; case NORMAL: taskPriority = 2; break; case HIGH: taskPriority = 3; break; case SEVERE: taskPriority = 4; break; } System.out.println("Task priority: " + taskPriority);
The enum Priority is declared as follows:
public enum Priority { LOW, NORMAL, HIGH, SEVERE }
From Java 14, you can use switch block as an expression. For example:
int taskPriority = switch (priority) { case LOW -> 1; case NORMAL -> 2; case HIGH -> 3; case SEVERE -> 4; }; System.out.println("Task priority: " + taskPriority);
Learn more: Java 14: Switch Expressions Enhancements Examples
Related Topics:
Other Recommended Tutorials:
- 9 Rules about Constructors in Java
- 12 Rules and Examples About Inheritance in Java
- 12 Rules of Overriding in Java You Should Know
- 10 Java Core Best Practices Every Java Programmer Should Know
- Understand Interfaces in Java
- Understand abstraction in Java
- Understand encapsulation in Java
- Understand inheritance in Java
- Understand polymorphism in Java
Comments