java - while 和 for 循环之间在计算上有什么区别吗?
问题描述
只是想知道之间是否有任何计算差异:
for(;condition;) {
//task
}
和
while(condition) {
//task
}
解决方案
在no difference
这两种情况下都有Java compiler generates the same byte code
。如果您在我使用时查看字节码for loop
:
0: bipush 11
2: istore_1
3: goto 9
6: iinc 1, -1
9: iload_1
10: bipush 10
12: if_icmpgt 6
15: return
上面的字节码为下面的代码生成:
int a = 11;
for (; a > 10;) {
a--;
}
和相同的字节码:
Code:
0: bipush 11
2: istore_1
3: goto 9
6: iinc 1, -1
9: iload_1
10: bipush 10
12: if_icmpgt 6
15: return
我使用时编译器生成的while loop
int a = 11;
while (a > 10) {
a--;
}
推荐阅读
- javascript - 当我从菜单中单击链接标签时如何使导航栏消失
- reactjs - Webpack-dev-server 在第一次文件更改后停止更新
- html - 引导轮播控件不适用于 node.js
- ruby-on-rails - Heroku 备份的 Postgres 本地恢复将日期时间字段提前 4 小时
- python - pytest中的monkeypatching boto3 s3调用
- r - emmeans 输出上的 plot.CLD 可以进行哪些自定义?
- java - Android 应用因 Android 广告 ID 政策而不断被拒绝
- excel - 如何使 Excel 中的替换函数仅在另一个 Excel 工作簿中第一次出现的单词中运行?
- python - 绑定到 tkinter 中每个键的键绑定
- mysql - 在 MySQL 中合并两个结果集