Fundamentals 4 min read

Why Java’s for(;;) and while(true) Loops Have No Performance Difference

The article explains that the infinite loop constructs for(;;) and while(true) in Java originate from C, discusses their syntax and usage, and shows through bytecode inspection that both compile to identical instructions, resulting in no measurable performance gap.

Architect's Tech Stack
Architect's Tech Stack
Architect's Tech Stack
Why Java’s for(;;) and while(true) Loops Have No Performance Difference

By searching the JDK8u source tree the author found 369 occurrences of for (;;) and 323 occurrences of while (true) , indicating both patterns are widely used.

The for (;;) syntax is inherited from C, where the loop header can omit the initialization, condition, and update expressions; using it to express an infinite loop avoids writing a magic constant like 1 in while (1) . Some developers still prefer while (true) , but both are functionally equivalent.

To compare their performance the author compiled two methods with javac (Oracle/Sun JDK8u / OpenJDK8u) and examined the generated bytecode.

Method using while (true) :

public void foo() {
    int i = 0;
    while (true) { i++; }
}

Method using for (;;) :

public void bar() {
    int i = 0;
    for (;;) { i++; }
}

Both methods produced identical bytecode (stack=1, locals=2, args_size=1):

0: iconst_0
1: istore_1
2: iinc 1, 1
5: goto 2

Since the Java compiler performs only minimal optimizations and generates the same bytecode for both loops, there is no performance difference at the bytecode level; any further differences would depend on the JVM’s JIT compiler, which sees identical input.

Thus, the choice between for (;;) and while (true) is a matter of style rather than efficiency.

JavaperformanceBytecodefor loopwhile looplanguage fundamentals
Architect's Tech Stack
Written by

Architect's Tech Stack

Java backend, microservices, distributed systems, containerized programming, and more.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.