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干什么? ...
随机推荐
- js中widow.open()方法详解
一. window.open() 支持环境: JavaScript1.0+/JScript1.0+/Nav2+/IE3+/Opera3+ 二.基本语法: window.open(pageURL,nam ...
- ceph主要数据结构解析3-Ceph_fs.h文件
(1)集群内部子版本协议类型宏定义:与公共协议保持独立性,以便消息类型和协议升级受影响 #define CEPH_OSDC_PROTOCOL 24 /* server/client */OSD服务 ...
- soundPool和audiofocus
audiofocus试验: 使用soundPool来写一个播放音频的porject. 资源初始化: setContentView(R.layout.activity_main); Button bt1 ...
- Java实现BASE64编解码
Java实现BASE64编解码 作者:chszs,转载需注明.博客主页:http://blog.csdn.net/chszs BASE64和其它类似的编码算法通经常使用于转换二进制数据为文本数据,其目 ...
- [转] 查看CPU使用率 top命令详解
一 top是一个动态显示过程,即可以通过用户按键来不断刷新当前状态.如果在前台执行该命令,它将独占前 台,直到用户终止该程序为止. 比较准确的说,top命令提供了实时的对系统处理器的状态监视.它将显示 ...
- Debian 桌面美化
Debian 桌面美化 安装 gnome-tweak-tool aptitude install gnome-tweak-tool 登陆gnome-look下载主题包 gnome-look上有很多主题 ...
- LA 6448 Credit Card Payment
[题目] 你的信用卡目前欠M元,每月的汇率是R,每月的利息要四舍五入为小数点后两位,你每月还B元,问多少月能还清. 输入 先是T代表测试数据组数 接下来T行,每行有三个实数,R,M,B每个实数小数 ...
- 2015-09-28 Javascript
1.Javascript是什么? JavaScript是一种脚本语言,结构简单,使用方便,其代码可以直接放入HTML文档中,可以直接在支持JavaScript的浏览器中运行.JavaSript. Ja ...
- Examples_07_06 无法下载android的sdk
在hosts里面配置. 74.125.237.1 dl-ssl.google.com 在AndroidManifest.xml中添加 <uses-feature android:name=&qu ...
- 极度郁闷的错误调试——ajax jquery
今天在写一个简单邮件验证的页面时,本来以为二十分钟的事情,却调试了一个半小时,简直郁闷,具体的错误如下: 在页面中,有一段如下的代码: <td colspan="3"> ...