Java: for(;;) vs. while(true)

JavaFor LoopWhile Loop

Java Problem Overview


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?

Java Solutions


Solution 1 - Java

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.

Solution 2 - Java

It's up to you which one to use. Cause they are equals to compiler.

create file:

// first test
public class Test {
    public static void main(String[] args) {
        while (true) {
            System.out.println("Hi");
        }
    }
}

compile:

javac -g:none Test.java
rename Test.class Test1.class

create file:

// second test
public class Test {
    public static void main(String[] args) {
        for (;;) {
            System.out.println("Hi");
        }
    }
}

compile:

javac -g:none Test.java
mv Test.class Test2.class

compare:

diff -s Test1.class Test2.class
Files Test1.class and Test2.class are identical

Solution 3 - Java

On Oracle Java 7 you get the same byte code. You cannot tell from the byte code which was using in the original. Which is best is a matter of taste. I use while(true)

Solution 4 - Java

It compiles down to the same byte code, and it is a matter of taste which construct you prefer.

I read a lot of source code from the Oracle distributed JDK, and I cannot easily remember that I've seen a while(true) statement in their code, but I have seen a whole lot of for(;;) statements in their code. Me personally, I favor for(;;) and my reasoning goes a bit like this:

The while-loop "should" require a boolean expression, not a boolean constant. while(true) is a bit like if(true), both of which I think has been made legal only for the added comfort of the masses. An empty while() does not compile. Adding the true in there feels a bit like hacking.

for(;;) on the other hand is a "real loop", albeit an empty one. Also, it saves you a couple of keystrokes! =) (not that it matter)

Therefore, and I know it sounds crazy, but although while(true) reads better in English, I think for(;;) better express your intent and is more akin to the Java programming language. Eternal loops should be avoided anyways. When I read for(;;), I feel secure knowing the developer will brake the execution path somewhere. When I read while(true), I just cannot be that sure anymore. But hey, maybe that's just me! We're all free to pick our own flavor.

Solution 5 - Java

Depending upon the compiler, it should map to the same byte code.

Solution 6 - Java

JVM will find the best way to make bytecode and in both cases should do the same.So I think there's no difference. while(true) is just prettier.

Solution 7 - Java

I have looked at the generated byte code and found that since the condition is always true (at compile time), the compiler will compile away the test and just branch always back to the top of the loop. I assume that a continue statement will also do a branch always back to the top of the loop. So, not only does it not make any difference, there isn't even any code generated to test anything.

Solution 8 - Java

Functionally there is no difference. Any efficiency gained or lost by a difference in bytecode will likely be insignificant compared to any instruction you would run in the body of the loop.

Solution 9 - Java

Only the difference is required time for the parser, the two distinct expressions include different number of tokens to be parsed however the computation time difference is very low and the compiled bytecode is the same for two cases.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionsjasView Question on Stackoverflow
Solution 1 - JavaÓscar LópezView Answer on Stackoverflow
Solution 2 - Java4ndrewView Answer on Stackoverflow
Solution 3 - JavaPeter LawreyView Answer on Stackoverflow
Solution 4 - JavaMartin AnderssonView Answer on Stackoverflow
Solution 5 - JavaDMCSView Answer on Stackoverflow
Solution 6 - Javashift66View Answer on Stackoverflow
Solution 7 - JavaBruce BartonView Answer on Stackoverflow
Solution 8 - JavaDevView Answer on Stackoverflow
Solution 9 - JavajbytecodeView Answer on Stackoverflow