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

What is the difference between the standard while(true) and for(;;) loops?

Is there any file or both will be mapped to the same bytecode after compilation?

+48
java for-loop while-loop
Jan 16 2018-12-12T00:
source share
8 answers

Semantically, they are completely equivalent. This is a matter of taste, but I think that while(true) looks cleaner and is easier to read and understand at a glance. In Java, none of them raise a compiler warning.

At the bytecode level, this may depend on the compiler and the optimization level, but in principle the emitted code should be the same.

EDIT:

In 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){} as follows:

  L0 LINENUMBER 6 L0 FRAME SAME GOTO L0 

So yes, at least for me, they are the same.

+83
Jan 16 2018-12-12T00:
source share

This is for you which one to use. Because they are equal to the compiler.

create file:

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

compilation:

 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"); } } } 

compilation:

 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 
+18
Jan 16 '12 at 14:00
source share

In Oracle Java 7, you get the same byte code. You cannot specify from the byte code that was used in the original. Best of all is a matter of taste. I use while(true)

+4
Jan 16 2018-12-12T00:
source share

It combines with the same byte code, and this is a matter of taste, which you prefer.

I read a lot of source code from the distributed JDK Oracle, and I can’t easily recall that I saw the while(true) statement in my code, but I saw many for(;;) statements in my code, I personally, I prefer for(;;) , and my reasoning is a bit like this:

The while loop should contain a boolean expression, not a boolean constant. while(true) bit like if(true) , both of which, I think, have become legal only for the added comfort of the masses. An empty while() does not compile. Adding true there is a bit like a hack.

for(;;) , on the other hand, is a "real loop", albeit empty. In addition, it saves you a couple of keystrokes! =) (not so important)

Therefore, and I know that it sounds crazy, but although (the truth) is better read in English, I think that for (;;) it is better to express your intentions and is more akin to the Java programming language. In any case, perpetual cycles should be avoided. When I read (;;), I feel safe knowing that the developer is slowing down the execution path somewhere. When I read (true), I just can't be sure. But hey maybe just me! We can all choose our own taste.

+4
May 5 '14 at 11:31
source share

Depending on the compiler, it must map to the same byte code.

+2
Jan 16 2018-12-16T00:
source share

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

+1
Jan 16 2018-12-12T00:
source share

Functionally, there is no difference. Any efficiency gained or lost by the difference in bytecode is likely to be negligible compared to any instruction that you execute in the body of the loop.

+1
Jan 16 2018-12-12T00:
source share

I looked at the generated bytecode and found that since the condition is always true (at compile time), the compiler will compile the test and just the branch will always go back to the beginning of the loop. I assume that the continue statement will also make the branch always back to the beginning of the loop. Thus, this not only does not matter, but nothing exists for testing.

+1
Jun 02 2018-12-12T00:
source share



All Articles