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干什么? ...
随机推荐
- 夏普比率(Sharpe Ratio)
投资中有一个常规的特点,即投资标的的预期报酬越高,投资人所能忍受的波动风险越高:反之,预期报酬越低,波动风险也越低.所以理性的投资人选择投资标的与投资组合的主要目的为:在固定所能承受的风险下,追求最大 ...
- Dubbo[一个分布式服务框架
http://alibaba.github.io/dubbo-doc-static/User+Guide-zh.htm#UserGuide-zh-API%E9%85%8D%E7%BD%AE http: ...
- StarUML中时序图添加小人
转载于 http://blog.csdn.net/longyuhome/article/details/9011629 在看时序图的例子的时候,发现有些的时序图上有小人的图标,可是一些UML工具却没有 ...
- Lombok介绍及使用方法
lombok简介 lombok是暑假来到公司实习的时候发现的一个非常好用的小工具,刚见到的时候就感觉非常惊艳,有一种相见恨晚的感觉,用了一段时间之后感觉的确挺不错,所以特此来推荐一下. lombok的 ...
- OKHttp源码解析
http://frodoking.github.io/2015/03/12/android-okhttp/ Android为我们提供了两种HTTP交互的方式:HttpURLConnection 和 A ...
- linux nohup命令
nohup 命令 用途:不挂断地运行命令.如果你正在执行一个job,并且你希望在退出帐户/关闭终端之后继续运行,可以使用nohup命令.nohup就是不挂起的意思( no hang up). 语法:n ...
- UVA 12378 Ball Blasting Game 【Manacher回文串】
Ball Blasting Game Morteza is playing a ball blasting game. In this game there is a chain of differe ...
- hdu 5124
bc上的题目,很水,有很多方法做吧,题意大概就是给定你票数,然后让你求出票数最多的那个下标...... 之前我用两个for循环分开写,一个是读入,然后是判断,提交就wa,后来网上看了别人的,就是不能分 ...
- css书写步骤
CSS整体书写步骤1:CSS RESET 重置2:CSS GLABAL 全局属性3: CSS分模块属性,(先写默认样式和设计图相差最大的部分,先大块布局,后细致调整) <style>/*C ...
- 关于ISAPI和CGI限制,这个要设为允许
否则程序就报这个错误,注意,设置允许时不是在添加的网站上设置,而是在根iis,选择后右侧出现关于ISAPI和CGI限制,进去后选择相应版本,设置为允许就可以了