java - 为什么我在这个 java for 循环中没有收到错误?
问题描述
在下面的语句中,我们将得到无法访问的语句错误。我知道该错误背后的原因是什么。
for(;false;)
System.out.println("Unrechable statement");
我的问题是为什么我们不会在下面的声明中出错。
boolean b = false;
for(;b;)
System.out.println("NO error");
解决方案
The reason is compiler will consider that there may be a chance that b
is being changes from its previous value to some other value by some other part of your program or thread, Even though your program is not actually changing b
but compiler will not detect it at compile time.
Clarification could be just make b
as final & see you will get the same error because compiler will notice that b
is final & can not be changed by other part of program so will consider false
as final value of b
& thus will produce error .
final boolean b = false;
for(;b;)
System.out.println("NO error"); //error: unreachable statement
推荐阅读
- java - 如何使用jpa在spring中创建没有@Id字段的表?
- validation - 如何验证导入的文件是否是 vue 中的 .csv 文件类型
- python - 使用 f2py 后不生成扩展模块 (*.so)
- sas - 选项而不是赋值
- java - AlertDialog.Builder 显示特定长度按钮选项的额外填充
- c - 孩子和父母在一棵树
- php - 带有 mongodb 的流明:出现错误:在 null 上调用成员函数 prepare()
- docker - Docker 日志输出和 nginx 访问和错误日志
- mysql - 查询适用于 PhpMyadmin 但不适用于我的服务器
- react-native - 删除屏幕上的导航标题以响应本机应用程序