Java switch语句 #
一、switch语句概述 #
switch语句是一种多分支选择结构,根据表达式的值选择执行对应的case分支。
二、基本语法 #
2.1 标准语法 #
java
switch (表达式) {
case 值1:
// 执行代码
break;
case 值2:
// 执行代码
break;
default:
// 默认执行代码
}
2.2 基本示例 #
java
int day = 3;
String dayName;
switch (day) {
case 1:
dayName = "星期一";
break;
case 2:
dayName = "星期二";
break;
case 3:
dayName = "星期三";
break;
case 4:
dayName = "星期四";
break;
case 5:
dayName = "星期五";
break;
case 6:
dayName = "星期六";
break;
case 7:
dayName = "星期日";
break;
default:
dayName = "无效日期";
}
System.out.println(dayName); // 星期三
三、switch支持的类型 #
3.1 支持的数据类型 #
- byte、short、int、char
- 枚举类型(enum)
- String(Java 7+)
- 包装类型:Byte、Short、Integer、Character
3.2 整数类型 #
java
int month = 2;
int days;
switch (month) {
case 1: case 3: case 5: case 7: case 8: case 10: case 12:
days = 31;
break;
case 4: case 6: case 9: case 11:
days = 30;
break;
case 2:
days = 28; // 简化处理
break;
default:
days = 0;
}
3.3 字符类型 #
java
char grade = 'B';
switch (grade) {
case 'A':
System.out.println("优秀");
break;
case 'B':
System.out.println("良好");
break;
case 'C':
System.out.println("及格");
break;
default:
System.out.println("其他");
}
3.4 字符串类型(Java 7+) #
java
String role = "admin";
switch (role) {
case "admin":
System.out.println("管理员权限");
break;
case "user":
System.out.println("用户权限");
break;
case "guest":
System.out.println("访客权限");
break;
default:
System.out.println("未知角色");
}
3.5 枚举类型 #
java
enum Direction {
NORTH, SOUTH, EAST, WEST
}
Direction dir = Direction.NORTH;
switch (dir) {
case NORTH:
System.out.println("向北");
break;
case SOUTH:
System.out.println("向南");
break;
case EAST:
System.out.println("向东");
break;
case WEST:
System.out.println("向西");
break;
}
四、case穿透 #
4.1 穿透现象 #
如果没有break,程序会继续执行下一个case,直到遇到break或switch结束。
java
int num = 2;
switch (num) {
case 1:
System.out.println("一");
case 2:
System.out.println("二");
case 3:
System.out.println("三");
default:
System.out.println("其他");
}
// 输出:
// 二
// 三
// 其他
4.2 利用穿透 #
java
int month = 2;
String season;
switch (month) {
case 3: case 4: case 5:
season = "春季";
break;
case 6: case 7: case 8:
season = "夏季";
break;
case 9: case 10: case 11:
season = "秋季";
break;
case 12: case 1: case 2:
season = "冬季";
break;
default:
season = "无效月份";
}
System.out.println(season); // 冬季
4.3 注意事项 #
java
// 不推荐:容易出错
switch (value) {
case 1:
doA();
case 2:
doB();
break;
}
// 推荐:明确使用break或注释说明穿透意图
switch (value) {
case 1:
doA();
// fall through
case 2:
doB();
break;
}
五、switch表达式(Java 12+) #
5.1 新语法 #
Java 12引入了switch表达式,使用箭头语法:
java
int day = 3;
String dayName = switch (day) {
case 1 -> "星期一";
case 2 -> "星期二";
case 3 -> "星期三";
case 4 -> "星期四";
case 5 -> "星期五";
case 6, 7 -> "周末";
default -> "无效日期";
};
System.out.println(dayName); // 星期三
5.2 多值匹配 #
java
int month = 2;
String season = switch (month) {
case 3, 4, 5 -> "春季";
case 6, 7, 8 -> "夏季";
case 9, 10, 11 -> "秋季";
case 12, 1, 2 -> "冬季";
default -> "无效月份";
};
5.3 yield关键字 #
在代码块中返回值使用yield:
java
int score = 85;
String grade = switch (score / 10) {
case 10, 9 -> "优秀";
case 8 -> {
System.out.println("良好");
yield "良好";
}
case 7 -> "中等";
case 6 -> "及格";
default -> "不及格";
};
六、switch vs if-else #
6.1 使用场景对比 #
| 特性 | switch | if-else |
|---|---|---|
| 判断类型 | 等值判断 | 等值判断、范围判断 |
| 支持类型 | 整数、字符、字符串、枚举 | 任意布尔表达式 |
| 可读性 | 多分支时更清晰 | 条件复杂时更灵活 |
| 性能 | 编译器优化(跳转表) | 顺序判断 |
6.2 选择建议 #
java
// 使用switch:多个等值判断
switch (day) {
case 1: case 7:
System.out.println("周末");
break;
default:
System.out.println("工作日");
}
// 使用if-else:范围判断或复杂条件
if (score >= 90) {
System.out.println("优秀");
} else if (score >= 60) {
System.out.println("及格");
} else {
System.out.println("不及格");
}
七、常见模式 #
7.1 菜单选择 #
java
int choice = 2;
switch (choice) {
case 1:
System.out.println("新增");
break;
case 2:
System.out.println("修改");
break;
case 3:
System.out.println("删除");
break;
case 4:
System.out.println("查询");
break;
case 0:
System.out.println("退出");
break;
default:
System.out.println("无效选择");
}
7.2 状态机 #
java
enum State {
IDLE, RUNNING, PAUSED, STOPPED
}
State state = State.RUNNING;
switch (state) {
case IDLE:
startProcess();
state = State.RUNNING;
break;
case RUNNING:
pauseProcess();
state = State.PAUSED;
break;
case PAUSED:
resumeProcess();
state = State.RUNNING;
break;
case STOPPED:
resetProcess();
state = State.IDLE;
break;
}
7.3 计算器 #
java
char operator = '+';
int a = 10, b = 5;
int result;
switch (operator) {
case '+':
result = a + b;
break;
case '-':
result = a - b;
break;
case '*':
result = a * b;
break;
case '/':
result = b != 0 ? a / b : 0;
break;
default:
result = 0;
}
八、注意事项 #
8.1 case值必须是常量 #
java
int x = 5;
int y = 10;
// 错误:case值必须是编译时常量
// switch (x) {
// case y: // 编译错误
// }
// 正确:使用字面量或final常量
final int CONSTANT = 5;
switch (x) {
case CONSTANT:
System.out.println("匹配");
break;
}
8.2 case值不能重复 #
java
int x = 5;
// 错误:case值重复
// switch (x) {
// case 5:
// System.out.println("一");
// break;
// case 5: // 编译错误
// System.out.println("二");
// break;
// }
8.3 null值处理 #
java
String s = null;
// 错误:switch表达式不能为null
// switch (s) {
// case "Hello":
// System.out.println("Hello");
// break;
// }
// 正确:先检查null
if (s != null) {
switch (s) {
case "Hello":
System.out.println("Hello");
break;
}
}
九、总结 #
| 特性 | 说明 |
|---|---|
| 支持类型 | byte、short、int、char、String、enum |
| case值 | 必须是编译时常量 |
| break | 防止穿透到下一个case |
| default | 默认分支,可省略 |
| 穿透 | 无break时继续执行下一个case |
switch语句要点:
- 适合多个等值判断
- 不要忘记break
- 利用穿透简化代码
- Java 12+可使用switch表达式
- null值需要单独处理
最后更新:2026-03-26