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分析的更多相关文章

  1. Gen中的switch分析及lookupswitch与tableswitch指令

    int chooseNear(int i) { switch (i) { case 0: return 0; case 1: return 1; case 2: return 2; default: ...

  2. Android 中图片压缩分析(上)

    作者: shawnzhao,QQ音乐技术团队一员 一.前言 在 Android 中进行图片压缩是非常常见的开发场景,主要的压缩方法有两种:其一是质量压缩,其二是下采样压缩. 前者是在不改变图片尺寸的情 ...

  3. Spring Cloud Data Flow 中的 ETL

    Spring Cloud Data Flow 中的 ETL 影宸风洛 程序猿DD 今天 来源:SpringForAll社区 1 概述 Spring Cloud Data Flow是一个用于构建实时数据 ...

  4. HanLP中人名识别分析

    HanLP中人名识别分析 在看源码之前,先看几遍论文<基于角色标注的中国人名自动识别研究> 关于命名识别的一些问题,可参考下列一些issue: 名字识别的问题 #387 机构名识别错误 关 ...

  5. HanLP中人名识别分析详解

    HanLP中人名识别分析详解 在看源码之前,先看几遍论文<基于角色标注的中国人名自动识别研究> 关于命名识别的一些问题,可参考下列一些issue: l ·名字识别的问题 #387 l ·机 ...

  6. Java 7 中的Switch 谈 Java版本更新和反编译知识

    Java 7 中的Switch 谈 Java版本更新和反编译知识          学习编程,享受生活,大家好,我是追寻梦的飞飞.今天主要讲述的是Java7中的更新Switch实现内部原理和JAD反编 ...

  7. java中的Switch case语句

    java中的Switch case 语句 在Switch语句中有4个关键字:switch,case break,default. 在switch(变量),变量只能是整型或者字符型,程序先读出这个变量的 ...

  8. Android中AppWidget的分析与应用:AppWidgetProvider .

    from: http://blog.csdn.net/thl789/article/details/7887968 本文从开发AppWidgetProvider角度出发,看一个AppWidgetPrv ...

  9. Java中的Switch用法

    一.java当中的switch与C#相比有以下区别 注:在java中switch后的表达式的类型只能为以下几种:byte.short.char.int(在Java1.6中是这样),   在java1. ...

随机推荐

  1. 记一次项目使用webuploader爬坑之旅

       因前端页面开发使用的为VUE开发,又要支持IE9,遂只有基于webuploader封装一个上传组件.地址:https://github.com/z719725611/vue-upload-web ...

  2. poj 2591 Set Definition

    题目 这道题是个水题,写博客上的人好像都不屑于写这这道题.但是我为什么还是觉得挺难的呢? 我也是看了别人的博客才过了的,感觉这种写法很新颖,这样就可以不用在排序了. 下面给出正解: #include& ...

  3. spring mvc学习笔记(一)web.xml文件配置的一点重要信息

    通过这个web.xml文件可以看出,所有的*.shtml的请求,都会被springmvc这个servlet处理.这里如果没有指定contextConfigLocation这个参数,将会按照默认规则在c ...

  4. Berlin 10.1 支持 iPhone 4 (iOS v7.x)

    http://www.cnblogs.com/onechen/p/5559017.html 原本在 Seattle 版本时,还能支持 iPhone 3GS (iOS v6.x), iPhone 4 ( ...

  5. linux系统编程之管道(三):命令管道(FIFO)

    一,匿名管道PIPE局限性 管道的主要局限性正体现在它的特点上: 只支持单向数据流: 只能用于具有亲缘关系的进程之间: 没有名字: 管道的缓冲区是有限的(管道制存在于内存中,在管道创建时,为缓冲区分配 ...

  6. 疑难杂症--SQL SERVER 2012下数据库内存异常回收

    --=================================================================== --背景: 在一台SQL SERVER 2012 SP1(1 ...

  7. Linux中目录proc/net/dev详解

    在Linux系统中,系统调用是操作系统提供给应用程序使用操作系统服务的重要接口,但同时也正是通过系统调用机制,操作系统屏蔽了用户直接访问系统内核的可能性.幸运的是Linux提供了LKM机制可以使我们在 ...

  8. JIT与JVM的三种执行模式:解释模式、编译模式、混合模式

    Java JIT(just in time)即时编译器是sun公司采用了hotspot虚拟机取代其开发的classic vm之后引入的一项技术,目的在于提高java程序的性能,改变人们“java比C/ ...

  9. (转)为什么要重写 hashcode 和 equals 方法?

    作者丨hsm_computer cnblogs.com/JavaArchitect/p/10474448.html 我在面试Java初级开发的时候,经常会问:你有没有重写过hashcode方法?不少候 ...

  10. 《Python绝技:运用Python成为顶级黑客》 Python实用小工具

    1.实现简单探测 使用socket模块,connect()方法建立与指定IP和端口的网络连接:revc(1024)方法将读取套接字中接下来的1024B数据 mport socket import sy ...