且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在java中使用while循环无法访问语句错误

更新时间:2023-01-16 21:37:50

前两个示例的主要区别在于,在第一种情况下,条件是一个常量,而在第二种情况下则不是.

The main difference between the first two examples is that in the first case, the condition is a constant, whereas in the second it is not.

例如,如果您将 boolean i = false; 更改为 final boolean i = false;,您将得到相同的编译错误,因为 i 现在是一个常量.

For example, if you change boolean i = false; into final boolean i = false;, you will get the same compile error because i is now a constant.

不可达语句的规则定义在JLS 14.21.特别是对 if 有特殊处理,允许 if(DEBUG) 结构,其中 DEBUG 可能是一个常量.

The rules for unreachable statements are defined in the JLS 14.21. In particular there is a special treatment for if to allow if(DEBUG) structures where DEBUG might be a constant.

至于do/while,里面的语句会执行一次,所以没有问题.

As for the do / while, the statement inside will be executed once so there is no problem.

这篇相关帖子中有关常量的更多详细信息.

More details about the constants in this related post.