Switch Statement

Use the switch statement to select one of many blocks of codes to be executed.

Syntax

A switch works with the byte, short, char, and int primitive data types. It also works with enumerated types, the String class, and a few special classes that wrap certain primitive types: Character, Byte, Short, and Integer.

Following code will name current month:

Run The Application

Right click to the SwitchDemo class; select Run As -> Java Application.

Output

Another example with String,

Run The Application

Right click to the SwitchDemo2 class; select Run As -> Java Application.

Output