if(false)vs while(false):无法访问的代码与死代码 [英] if(false) vs. while(false): unreachable code vs. dead code

查看:211
本文介绍了if(false)vs while(false):无法访问的代码与死代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Eclipse中尝试了以下内容:

I tried the following in Eclipse:


  • if(false){} :警告'死代码'

  • while(false){} :编译错误'无法访问代码'

  • if (false) {}: warning 'dead code'
  • while (false) {}: compilation error 'unreachable code'

我想知道这种差异是否存在真正的原因。我已经发现了这个......

I was wondering whether there is a real 'reason' for this difference. I already found this...

无法访问的代码编译器错误

...但为什么不允许而(false)用于相同的调试目的?

...but why not allow while (false) for the same debugging purpose?

推荐答案

关于无法访问代码的JLS部分解释了基本原理。从本质上讲,Java通常不应该像C一般使用 #ifdef 那样使用条件编译,但是在某些情况下(例如调试,特别是向后二进制兼容性)允许编译器需要完全删除代码,因此特定的构造 if(false)是允许的。

The JLS section on unreachable code explains the rationale. Essentially, Java normally shouldn't use conditional compilation like C routinely does with #ifdef, but there are some situations (such as debugging, and in particular backward binary compatibility) where allowing the compiler to entirely strip out code is needed, and so the specific construct if(false) is permitted for that purpose.

这篇关于if(false)vs while(false):无法访问的代码与死代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆