我的Java日记(一)unreachable code & dead code

#Why this topic?

偶然在Joe的blog上看到Java Puzzle:unreachable statement这篇文章,没有考虑过这个问题,所以认真学习一下,在stackoverflow上也找到了一个类似的稍微简化的版本

#What happens?


作者在这篇文章中问,猜测在下面的三种情况下,会发生什么事情呢?Java的编译器会给什么反应呢?

public static void main(String[] args) {
    return;
    System.out.println("Hi!");    
}

 

public static void main(String[] args) {
        if (true) {
            return;
        }
        System.out.println("Hi!");    
    }

 

public static void main(String[] args) {
        while (true) {
            return;
        }
        System.out.println("Hi!");    
    }

 

在Eclipse里面试了一下,很奇怪的,第二个snippet竟然和第一个、第三个表现的不一样。

    

#How could this happen?

不管为什么这样,先查查dead code和unreachable code什么情况好了。

摘自Wiki的Dead code和unreachable code的关系。

Dead code includes code that can never be executed (unreachable code), and code that only affects dead variables, that is, variables that are irrelevant to the program.

再查查Java Language Specification怎么定义unreachable吧(pp. 411)。

有关Unreachable statements的第一句话就是:

It is a compile-time error if a statement cannot be executed because it is unreachable.

看来unreachable code直接就造成compile-time error了。

Reachable的定义是: There must be some possible execution path from the beginning of the constructor, method, instance initializer, or static initializer that contains the statement to the statement itself.

有关While的定义部分,如果While能够complete normally,那么其后接着的代码就是reachable的了。

A while statement can complete normally iff at least one of the following is true:

  • The while statement is reachable and the condition expression is not a constant expression with value true.
  • There is a reachable break statement that exits the while statement.

The contained statement is reachable iff the while statement is reachable and the condition expression is not a constant expression whose value is false.

这里就是解释啦,如果条件表达式是值为true的常量表达式,除非while循环体里有reachable的break退出while,否则while就不能normally completed,后面的代码就是unreachable的了。

那么if是怎么定义的呢?

However, in order to allow the if statement to be used conveniently for "conditional compilation" purposes, the actual rules differ.

一看到however就知道事情不太妙了!果然if是特殊的。

The rules for the if statement are as follows:

  • An if-then statement can complete normally iff it is reachable.

The then-statement is reachable iff the if-then statement is reachable.

  • An if-then-else statement can complete normally iff the then-statement can complete normally or the else-statement can complete normally.

The then-statement is rechable iff the if-then-else statement is reachable.

The else-statement is reachable iff the if-then-else statement is reachable.

从上面可以看出,if的处理中不会去管条件表达式是不是true,只要if是reachable的,就认为能够complete normally。

所以终于搞清楚第二个snippet为什么是dead code warning,而不是unreachable code error了。

这时候回过头来想为什么单单if要这么设计呢?JLS也给出了解释,就是为了方便程序员调试。

static final boolean DEBUG = false;

if (DEBUG) {
    //code block
}

只要修改DEBUG的值,就不用增删代码块了。

 

转载于:https://www.cnblogs.com/jingtianqiwen/archive/2012/10/12/2721496.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值