Java: for(;;) vs. while(true)
What is the difference between a standard while(true) loop and for(;;)?
Is there any, or will both be mapped to the same bytecode after compiling?
Semantically, they're completely equivalent. It's a matter of taste, but I think while(true) looks cleaner, and is easier to read and understand at first glance. In Java neither of them causes compiler warnings.
At the bytecode level, it might depend on the compiler and the level of optimizations, but in principle the code emitted should be the same.
EDIT:
On my compiler, using the Bytecode Outline plugin,the bytecode for for(;;){} looks like this:
L0
LINENUMBER 6 L0
FRAME SAME
GOTO L0
And the bytecode for while(true){} looks like this:
L0
LINENUMBER 6 L0
FRAME SAME
GOTO L0
So yes, at least for me, they're identical.
It's up to you which one to use. Cause they are equals to compiler.
public class Test {
public static void main(String[] args) {
while (true) {
System.out.println("Hi");
}
}
}
javac -g:none Test.java
rename Test.class Test1.class
public class Test {
public static void main(String[] args) {
for (;;) {
System.out.println("Hi");
}
}
}
# javac -g:none Test.java
# mv Test.class Test2.class
# diff -s Test1.class Test2.class
Files Test1.class and Test2.class are identical
http://stackoverflow.com/questions/8880870/java-for-vs-whiletrue
Java: for(;;) vs. while(true)的更多相关文章
- java.util.Arrays.useLegacyMergeSort=true 作用
(原) 今天看了一下现场的环境,发现有个其它部门的项目用到了这样一个参数: -Djava.util.Arrays.useLegacyMergeSort=true 于是查看了一下什么作用. 在JDK1. ...
- java中关于while(true)的理解
java中while(true)的理解: while(true)作为无限循环,经常在不知道循环次数的时候使用,并且需要在循环内使用break才会停止,且在run()方法中基本都会写while(true ...
- Java中的while(true)
while(true)是一个无限循环 在内部用break或return退出循环,否则一直循环
- java构造函数重载this(true)
看storm的代码的时候,发现这样一句java代码, 很是不理解 google之后,发现原来是java语法中,构造函数重载,this()调用的其实就是 构造函数.This is constructor ...
- 这些年一直记不住的 Java I/O
参考资料 该文中的内容来源于 Oracle 的官方文档.Oracle 在 Java 方面的文档是非常完善的.对 Java 8 感兴趣的朋友,可以从这个总入口 Java SE 8 Documentati ...
- Using Headless Mode in the Java SE Platform--转
原文地址: By Artem Ananiev and Alla Redko, June 2006 Articles Index This article explains how to use ...
- Java命令行解析:apache commons-cli
http://commons.apache.org/proper/commons-cli/usage.html Apache Commons CLI用于解析命令行选项,也可以输出详细的选项说明信息. ...
- java SSH框架详解(面试和学习都是最好的收藏资料)
Java—SSH(MVC)1. 谈谈你mvc的理解MVC是Model—View—Controler的简称.即模型—视图—控制器.MVC是一种设计模式,它强制性的把应用程序的输入.处理和输出分开.MVC ...
- 聊一下C#开发者如何过渡到JAVA 开发者
由于工作需要,最近也开始倒腾Java了.NET的话,从2012年测试版开始玩的,那个时候VB6比较熟悉,还天真的以为VB.NET以后会很火, 事实证明,也只是一厢情愿,有C#了,要VB.NET干什么? ...
随机推荐
- windows 编程—— 使用函数笔记
目录: 创建滚动条 滚动条函数(新老版本) 取得设备内容句柄hdc 设置 hdc 中的属性 画点画线 画填充图形 使用自定义的 画笔 和 画刷 矩形.区域和剪裁 关于GDI映像模式 其他常用的方便计算 ...
- SOFTWARE_INTRODUCE_02
几款网页数据抓取软件 近年来,随着国内大数据战略越来越清晰,数据抓取和信息采集系列产品迎来了巨大的发展机遇,采集产品数量也出现迅猛增长.然而与产品种类快速增长相反的是,信息采集技术相对薄弱.市场竞争激 ...
- Android学习中R文件中途消失
新建工程的时候R文件明明是在的,结果等我做着做着,R.java不见了????于是我就上网查了查,发现,诶,大家都说的几种常见情况都试过了,1.对工程clean一下,选project->clean ...
- 静态代码检查工具 cppcheck 的使用
CppCheck是一个C/C++代码缺陷静态检查工具.不同于C/C++编译器及其它分析工具,CppCheck只检查编译器检查不出来的bug,不检查语法错误.所谓静态代码检查就是使用一个工具检查我们 ...
- 第四章:使用Proxy代理让客户端服务端分工合作。
<基于1.8 Forge的Minecraft mod制作经验分享> 别被那个Proxy代理吓到,很简单的. 我们先讨论为什么要用Proxy代理: 像打开新的UI这种操作,比如打开一个背包, ...
- 选课 树形dp+路径输出
#include<iostream> #include<cstdio> #include<cstring> #define maxn 2010 using name ...
- c# 语句关键字
1. yield 它表明一个方法,操作符或者get选择器里会出现迭代. 用yield return 同时返回每一个元素, 返回类型必须是IEnumerable, IEnumerable<T&g ...
- SVN库迁移整理方法总结
有时候需要从一台机器迁移svn存储库到另外一台机器,如果数据量非常大的话,没有好的方法是很不方便的,其实迁移svn跟迁移mysql差不多,也有导出导入的方案 以下是subversion官方推荐的备份方 ...
- Swift开发必备技巧:内存管理、weak和unowned
因为 Playground 本身会持有所有声明在其中的东西,因此本节中的示例代码需要在 Xcode 项目环境中运行.在 Playground 中可能无法得到正确的结果. 不管在什么语言里,内存管理的内 ...
- js兼容性大全(持续更新)
javascript部分 1. document.form.item 问题 问题: 代码中存在 document.formName.item("itemName") 这样的语句,不 ...