Switch Statement in Java

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

The switch expression work with primitive byte short int, char and with its wrapper class, enum type and String Object.

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.

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

Switch statement with integer

Output

Switch Statement with Byte Wrapper class

Switch statement character expression

Result

String Object in Switch Statement

From Java 7 String object is added with switch statement

Result

Enum in Switch Statement

Result

Java Switch program to print number of days in month