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)的更多相关文章

  1. java.util.Arrays.useLegacyMergeSort=true 作用

    (原) 今天看了一下现场的环境,发现有个其它部门的项目用到了这样一个参数: -Djava.util.Arrays.useLegacyMergeSort=true 于是查看了一下什么作用. 在JDK1. ...

  2. java中关于while(true)的理解

    java中while(true)的理解: while(true)作为无限循环,经常在不知道循环次数的时候使用,并且需要在循环内使用break才会停止,且在run()方法中基本都会写while(true ...

  3. Java中的while(true)

    while(true)是一个无限循环 在内部用break或return退出循环,否则一直循环

  4. java构造函数重载this(true)

    看storm的代码的时候,发现这样一句java代码, 很是不理解 google之后,发现原来是java语法中,构造函数重载,this()调用的其实就是 构造函数.This is constructor ...

  5. 这些年一直记不住的 Java I/O

    参考资料 该文中的内容来源于 Oracle 的官方文档.Oracle 在 Java 方面的文档是非常完善的.对 Java 8 感兴趣的朋友,可以从这个总入口 Java SE 8 Documentati ...

  6. Using Headless Mode in the Java SE Platform--转

    原文地址: By Artem Ananiev and Alla Redko, June 2006     Articles Index This article explains how to use ...

  7. Java命令行解析:apache commons-cli

    http://commons.apache.org/proper/commons-cli/usage.html Apache Commons CLI用于解析命令行选项,也可以输出详细的选项说明信息. ...

  8. java SSH框架详解(面试和学习都是最好的收藏资料)

    Java—SSH(MVC)1. 谈谈你mvc的理解MVC是Model—View—Controler的简称.即模型—视图—控制器.MVC是一种设计模式,它强制性的把应用程序的输入.处理和输出分开.MVC ...

  9. 聊一下C#开发者如何过渡到JAVA 开发者

    由于工作需要,最近也开始倒腾Java了.NET的话,从2012年测试版开始玩的,那个时候VB6比较熟悉,还天真的以为VB.NET以后会很火, 事实证明,也只是一厢情愿,有C#了,要VB.NET干什么? ...

随机推荐

  1. Highcharts下载与使用_数据报表图

    Highcharts简介 Highcharts:功能强大.开源.美观.图表丰富.兼容绝大多数浏览器的纯js图表库 Highcharts是一款纯javascript编写的图表库,能够很简单便捷的在Web ...

  2. centos 7 卸载 mariadb 安装mysql

    1,卸载mariadbsystemctl stop mariadbrpm -qa | grep mariadbrpm -e --nodeps mariadb-5.5.52-1.el7.x86_64rp ...

  3. 【转】非常完善的Log4net详细说明

    转自:http://www.cnblogs.com/zhangchenliang/p/4546352.htmlhttp://www.cnblogs.com/zhangchenliang/p/45463 ...

  4. Oracle 数据库 Database Express Edition 11g Release 2 (11.2) 错误解决集锦(安装方面)

    前言:第一次接触数据库,想下载个oracle试玩下(虽然听说一般大企业才用),到 官网下载 了个简易版 XE 版本,安装时要注意记住自己输入的数据库密码(口令)  还有安装路径不能含有空格(Do no ...

  5. GDB错误:Cannot find bounds of current function

    http://blog.csdn.net/zoomdy/article/details/17249165 mingdu.zheng <at> gmail <dot> com 使 ...

  6. js转码和解码兼容低版本火狐

    function HTMLEncode(html) {                var temp = document.createElement("div");       ...

  7. python基础--杂项

    字符串格式化: ython的字符串格式化有两种方式: 百分号方式.format方式 百分号的方式相对来说比较老,而format方式则是比较先进的方式,企图替换古老的方式,目前两者并存.[PEP-310 ...

  8. vmware-tools(vmware workstation 10.0.4)安装的时候遇到的bug

    有个GitHub,专门解决C++编译的时候出的问题 地址

  9. java语言基础特性

    使用Java5中的类型安全枚举和注解(以及元注解)编程 http://www.importnew.com/11053.html

  10. 修改登录linux之后显示的默认文件夹目录

    命令如下: ll -a vim .bash_profile 最后一行加上cd 需要显示的文件夹