Flow中的Switch分析
A switch statement can complete normally iff at least one of the following is true:
(1)The switch block is empty or contains only switch labels.
(2)The last statement in the switch block can complete normally.
(3)There is at least one switch label after the last switch block statement group.
(4)The switch block does not contain a default label.
(5)There is a reachable break statement that exits the switch statement.
16.2.9. switch Statements
1、V is [un]assigned after a switch statement (§14.11) iff all of the following are true:
(1)Either there is a default label in the switch block or V is [un]assigned after the switch expression.
(2)Either there are no switch labels in the switch block that do not begin a block-statement-group (that is, there are no switch labels immediately before the "}" that ends the switch block) or V is [un]assigned after the switch expression.
(3)Either the switch block contains no block-statement-groups or V is [un]assigned after the last block-statement of the last block-statement-group.
(4)V is [un]assigned before every break statement that may exit the switch statement.
2、V is [un]assigned before the switch expression iff V is [un]assigned before the switch statement.
If a switch block contains at least one block-statement-group, then the following rules also apply:
(1)V is [un]assigned before the first block-statement of the first block-statement-group in the switch block iff V is [un]assigned after the switch expression.
(2)V is [un]assigned before the first block-statement of any block-statement-group other than the first iff V is [un]assigned after the switch expression and V is [un]assigned after the preceding block-statement.
上面标红的4个条件要全部满足才能确定switch语句中的明确与非明确赋值。
满足条件的实例1:
class AA {
public AA(int i) { }
String p = "";
}
public void md(String p) {
int i;
switch (new AA(i=2).p) {
default:
i = 0;
case "b":
break;
}
int j = i; // 报错,i没有初始化
}
不满足条件的实例1:
public void md(String p) {
int i;
switch (p) {
default:
i = 0;
case "b":
}
int j = i; // 报错,i没有初始化
}
不满足条件的实例2:
public void t(String p){
switch(p){
default:
System.out.println("default");
case "a":
System.out.println("a");
}
}
将default放在源代码开始处,当p为任意类型的字符串时,包括空字符串都打印 default a
但是不能为null,因为switch传递的参数不允许为null。
如下的情况会报错,i在使用时没有被初始化。
情况1:
public void t(String p){
int i;
switch(p){
default:
i = 0;
System.out.println("default");
case "a":
System.out.println("a");
}
int j = i;
}
情况2:
public void t(String p){
int i;
switch(p){
case "a":
System.out.println("a");
break;
default:
i = 0;
System.out.println("default");
}
int j = i;
}
但是下面的一些情况就不会报错。
情况1:
public void t(String p){
int i;
switch(p){
case "a":
System.out.println("a");
default:
i = 0;
System.out.println("default");
}
int j = i;
}
情况2:
public void t(String p){
int i;
switch(p){
case "a":
i = 0;
System.out.println("a");
break;
default:
i = 0;
System.out.println("default");
}
int j = i;
}
当case分支中不加花括号时,其作用域可以延生到后面的case,如下:
public void t(String p){
switch(p){
case "a":
int i = 0;
System.out.println("a");
default:
i = 3;
System.out.println("default");
}
}
当加上花括号时就不允许这样,如下:
public void t(String p){
switch(p){
case "a":{
int i = 0;
System.out.println("a");
}
default:
i = 3;
System.out.println("default");
}
}
i=3的语句会报错,变量找不到。
public void t(String p){
int i = 0;
switch(p){
case "a":
int i = 0; // 报错,重复定义变量i,加花括号也同样报错
System.out.println("a");
default:
i = 3;
System.out.println("default");
}
}
这个很好理解,就是在局部变量作用域内,不能再定义相同名称的局部变量。
Flow中的Switch分析的更多相关文章
- Gen中的switch分析及lookupswitch与tableswitch指令
int chooseNear(int i) { switch (i) { case 0: return 0; case 1: return 1; case 2: return 2; default: ...
- Android 中图片压缩分析(上)
作者: shawnzhao,QQ音乐技术团队一员 一.前言 在 Android 中进行图片压缩是非常常见的开发场景,主要的压缩方法有两种:其一是质量压缩,其二是下采样压缩. 前者是在不改变图片尺寸的情 ...
- Spring Cloud Data Flow 中的 ETL
Spring Cloud Data Flow 中的 ETL 影宸风洛 程序猿DD 今天 来源:SpringForAll社区 1 概述 Spring Cloud Data Flow是一个用于构建实时数据 ...
- HanLP中人名识别分析
HanLP中人名识别分析 在看源码之前,先看几遍论文<基于角色标注的中国人名自动识别研究> 关于命名识别的一些问题,可参考下列一些issue: 名字识别的问题 #387 机构名识别错误 关 ...
- HanLP中人名识别分析详解
HanLP中人名识别分析详解 在看源码之前,先看几遍论文<基于角色标注的中国人名自动识别研究> 关于命名识别的一些问题,可参考下列一些issue: l ·名字识别的问题 #387 l ·机 ...
- Java 7 中的Switch 谈 Java版本更新和反编译知识
Java 7 中的Switch 谈 Java版本更新和反编译知识 学习编程,享受生活,大家好,我是追寻梦的飞飞.今天主要讲述的是Java7中的更新Switch实现内部原理和JAD反编 ...
- java中的Switch case语句
java中的Switch case 语句 在Switch语句中有4个关键字:switch,case break,default. 在switch(变量),变量只能是整型或者字符型,程序先读出这个变量的 ...
- Android中AppWidget的分析与应用:AppWidgetProvider .
from: http://blog.csdn.net/thl789/article/details/7887968 本文从开发AppWidgetProvider角度出发,看一个AppWidgetPrv ...
- Java中的Switch用法
一.java当中的switch与C#相比有以下区别 注:在java中switch后的表达式的类型只能为以下几种:byte.short.char.int(在Java1.6中是这样), 在java1. ...
随机推荐
- 使用idea,springboot,springsession,redis实现分布式微服务的session 共享
本次开发环境:idea2016.3.4 +jdk1.8+maven3.3.9+redis+springboot+jedis 本文中的项目使用Maven来管理项目依赖,使用Spring Session和 ...
- (KMP Next的运用) Period II -- fzu -- 1901
http://acm.fzu.edu.cn/problem.php?pid=1901 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=703 ...
- console的所有用法
http://jingyan.baidu.com/article/e75aca855c6419142edac6c1.html 参考它. console.info(), console.debug() ...
- TCP协议通讯流程
刚才网上找到的,觉得挺详细的,转来. tcp连接的三次握手大家肯定都熟了,可是有的人不一定对tcp断开的四次握手也很熟悉. 我在园子里面找到一张图,介绍的很好,现在转来!(该图片原博客地址:http: ...
- asp.net excel导出功能
以下是我在项目开发中所做的关于Excel导出功能,不足之处还望大家指正,相互学习 protected void btn_Export_Click(object sender, EventArgs e) ...
- ASP.NET webform多次提交表单问题
最近几天遇到一个头疼的问题,项目采用的是webform开发,每个界面都有个提交按钮,点击多次提交按钮导致提交按钮的OnClick事件执行了多次, 每次OnClick里面都有一些逻辑处理,执行了多次导致 ...
- 使用C#创建windows服务续之使用Topshelf优化Windows服务
前言: 之前写了一篇“使用C#创建windows服务”,https://www.cnblogs.com/huangwei1992/p/9693167.html,然后有博友给我推荐了一个开源框架Tops ...
- Restframework 渲染器 render 组件实例-4
渲染器默认存放位置: 在默认配置下 default-settings里 (APIVIEW点击去--> 1. renderer_classes = api_settings.DEFAULT_REN ...
- H - The LCIS on the Tree HDU - 4718
The LCIS on the Tree Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Oth ...
- 用redis统计大量用户的登陆情况[只判断是否活跃]
有这样的一个场景需求:有上亿的用户,要统计这批用户的登陆情况,例如一周连续登陆,连续三天是是否登陆,一周活跃天数等用户 存在的挑战 数据如何尽可能用小的空间存储 如何能快速获取指定的数据 如果使用文件 ...