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
时间: 2024-07-28 22:30:48

Java: for(;;) vs. while(true)的相关文章

Java中对while(true)的理解

while(true)作为无限循环,经常在不知道循环次数的时候使用,并且需要在循环内使用break才会停止. run()方法中基本都会写while(true)的循环,如下: public class XXXThread implements Runnable { public void run(){ while(true) { …… } } } 使用while(true)的作用: run方法中的代码就是线程要运行的代码,运行完毕以后,就不会再次运行,其方法本身并不是无限循环的.而while(tr

Java学习之IO流一

01. IO技术概述 * a: Output * 把内存中的数据存储到持久化设备上这个动作称为输出(写)Output操作 * b: Input * 把持久设备上的数据读取到内存中的这个动作称为输入(读)Input操作 * c: IO操作 * 把上面的这种输入和输出动作称为IO操作 02. File类的概述和作用 * A:File类的概述和作用 * a: File的概念 * File类是文件和目录路径名的抽象表示形式 * Java中把文件或者目录(文件夹)都封装成File对象 * 我们要去操作硬盘

Java Connection.setAutoCommit

Java setAutoCommit 默认为true,即每条SQL语句在各自的一个事务中执行. 很多时候需要有多个操作在一个事务执行,如循环插入,此时可在插入开始前设置 conn.setAutoCommit(false) ,插入结束后才conn.commit(),并在catch中执行conn.rollback().这样即使插入中间报错,前面执行没报错的也会回滚,从而保证数据的完整性.正确写法示例如下: 1 Connection con = null; 2 try { 3 con = getCon

java俄罗斯方块

代码如下 <span style="font-size:18px;">package com.model; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import javax.swing.*; impo

Java NIO 系列教程(转)

原文中说了最重要的3个概念,Channel 通道Buffer 缓冲区Selector 选择器其中Channel对应以前的流,Buffer不是什么新东西,Selector是因为nio可以使用异步的非堵塞模式才加入的东西.以前的流总是堵塞的,一个线程只要对它进行操作,其它操作就会被堵塞,也就相当于水管没有阀门,你伸手接水的时候,不管水到了没有,你就都只能耗在接水(流)上.nio的Channel的加入,相当于增加了水龙头(有阀门),虽然一个时刻也只能接一个水管的水,但依赖轮换策略,在水量不大的时候,各

[笔记&amp;轮子]java源码 生成本地javadoc api文档

在用Eclipse写java代码时候,有时候因为不知道一个java函数的作用,会通过把鼠移动到java函数上,如果它有javadoc的相关内容就会显示出来.但是并非所有java代码都有javadoc:即使安装了javadoc,在eclipse中如果不进行设定,也可能无法使用. 我在win7下安装的是javase的jdk,发现eclipse中默认的javadoc路径是http://download.oracle.com/javase/7/docs/api/,显然这是一个在线资源,问题是网络总是不稳

java 十宗罪

非常抱歉,第1点关于"abc"=="abc"返回false是我搞错了,在java里是返回true的,我没有经过实验就这么说了,非常抱歉.之所以会这么说,是因为不记得是哪一种,因为我使用了==进行了字符串的比较,结果并没有返回我相要的结果,然后请教老人,老人们就训诫道:最好使用equals进行比较,而不要使用==,故有此感慨. 好吧,我知道看这标题很多人就忍不住要拍砖或表示不赞同了,我都接受. 我在遇到问题时,找一些搞java朋友求助,有能解决的,我想说的是不能解决的

eclipse进行Debug的时候,发出“java breakpoint unable to install breakpoint”错误

错误情况图: 问题的解决方法: 直接点击忽略掉:Don't tell me again 来自网上的答案~~ I had the same error message in Eclipse 3.4.1, SUN JVM1.6.0_07 connected to Tomcat 6.0 (running in debug-mode on a different machine, Sun JVM1.6.0_16, the debug connection did work correctly). Win

Java NIO 系列教程

转载于http://www.iteye.com/magazines/132-Java-NIO Java NIO(New IO)是从Java 1.4版本开始引入的一个新的IO API,可以替代标准的Java IO API.本系列教程将有助于你学习和理解Java NIO.感谢并发编程网的翻译和投递. (关注ITeye官微,随时随地查看最新开发资讯.技术文章.) Java NIO提供了与标准IO不同的IO工作方式: Channels and Buffers(通道和缓冲区):标准的IO基于字节流和字符流