Flow中的Switch分析

 

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");
	}
}

 

这个很好理解,就是在局部变量作用域内,不能再定义相同名称的局部变量。  

 

 

 

 

 

 

 

 

 

  

转载于:https://www.cnblogs.com/extjs4/p/9456070.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值