Java switch case Statement
The switch case statement determines whether a variable is equal to one of a series of values, each of which is called a branch.
Syntax
The syntax of the switch case statement is as follows:
switch
(
expression
)
{
case
value
:
//
Statement
break
;
//
Optional
case
value
:
//
Statement
break
;
//
Optional
//
You can have any number of case statements
default
:
//
Optional
//
Statement
}

The switch case statement has the following rules:
-
The variable type in a switch statement can be: byte, short, int, or char. Starting with Java SE 7, switch supports the String type, and the case labels must be string constants or literals.
-
A switch statement can have multiple case statements. Each case is followed by a value to compare and a colon.
-
The data type of the value in a case statement must be the same as the data type of the variable, and it can only be a constant or a literal constant.
-
When the value of the variable equals the value of a case statement, the statements after that case statement begin to execute, and the switch statement is exited only when a break statement appears.
-
When a break statement is encountered, the switch statement terminates. The program jumps to the statement following the switch statement. A case statement does not have to contain a break statement. If no break statement appears, the program continues executing the next case statement until a break statement appears.
-
A switch statement can contain a default branch, which is usually the last branch of the switch statement (it can be in any position, but the last is recommended). The default branch executes when no case value equals the variable's value. The default branch does not require a break statement.
When switch case executes, it first performs matching; on a successful match it returns the value of the current case, then depending on whether there is a break, decides whether to continue outputting or to exit the judgment.
Example
Test.java File Code:
public
class
Test
{
public
static
void
main
(
String
args
[
]
)
{
//
char grade = args[0].charAt(0);
char
grade
=
'
C
'
;
switch
(
grade
)
{
case
'
A
'
:
System
.
out
.
println
(
"
Excellent
"
)
;
break
;
case
'
B
'
:
case
'
C
'
:
System
.
out
.
println
(
"
Good
"
)
;
break
;
case
'
D
'
:
System
.
out
.
println
(
"
Pass
"
)
;
break
;
case
'
F
'
:
System
.
out
.
println
(
"
You need to work harder
"
)
;
break
;
default
:
System
.
out
.
println
(
"
Unknown grade
"
)
;
}
System
.
out
.
println
(
"
Your grade is
"
grade
)
;
}
}
The compilation and execution results of the above code are as follows:
良好
你的等级是 C
If a case block has no break statement, the JVM does not output the return value of every case in sequence; instead it continues matching, and if no match succeeds it returns the default case.
Test.java File Code:
public
class
Test
{
public
static
void
main
(
String
args
[
]
)
{
int
i
=
5
;
switch
(
i
)
{
case
0
:
System
.
out
.
println
(
"
0
"
)
;
case
1
:
System
.
out
.
println
(
"
1
"
)
;
case
2
:
System
.
out
.
println
(
"
2
"
)
;
default
:
System
.
out
.
println
(
"
default
"
)
;
}
}
}
The compilation and execution results of the above code are as follows:
default
If a case block has no break statement, after a successful match, starting from the current case, the values of all subsequent cases will be output.
Test.java File Code:
public
class
Test
{
public
static
void
main
(
String
args
[
]
)
{
int
i
=
1
;
switch
(
i
)
{
case
0
:
System
.
out
.
println
(
"
0
"
)
;
case
1
:
System
.
out
.
println
(
"
1
"
)
;
case
2
:
System
.
out
.
println
(
"
2
"
)
;
default
:
System
.
out
.
println
(
"
default
"
)
;
}
}
}
The compilation and execution results of the above code are as follows:
1
2
default
If the currently matched case block has no break statement, starting from the current case, the values of all subsequent cases will be output; if a subsequent case block has a break statement, the judgment is exited.
Test.java File Code:
public
class
Test
{
public
static
void
main
(
String
args
[
]
)
{
int
i
=
1
;
switch
(
i
)
{
case
0
:
System
.
out
.
println
(
"
0
"
)
;
case
1
:
System
.
out
.
println
(
"
1
"
)
;
case
2
:
System
.
out
.
println
(
"
2
"
)
;
case
3
:
System
.
out
.
println
(
"
3
"
)
;
break
;
default
:
System
.
out
.
println
(
"
default
"
)
;
}
}
}
The compilation and execution results of the above code are as follows:
1
2
3